Is there a more efficient threading lock?
Weatherby,Gerard
gweatherby at uchc.edu
Sat Feb 25 16:47:00 EST 2023
“I'm no expert on locks, but you don't usually want to keep a lock while
some long-running computation goes on. You want the computation to be
done by a separate thread, put its results somewhere, and then notify
the choreographing thread that the result is ready.”
Maybe. There are so many possible threaded application designs I’d hesitate to make a general statement.
The threading.Lock.acquire method has flags for both a non-blocking attempt and a timeout, so a valid design could include a long-running computation with a main thread or event loop polling the thread. Or the thread could signal a main loop some other way.
I’ve written some code that coordinated threads by having a process talk to itself using a socket.socketpair. The advantage is that you can bundle multiple items (sockets, file handles, a polling timeout) into a select.select call which waits without consuming resources (at least on Linux) until
something interesting happens.
More information about the Python-list
mailing list