I had some annoyances with spawning subprocesses, like getting correct output and so on. A wrapper library, envoy, solved all of my problems with an easy-to-use interface that gets rid of most problems.
Using thread, I sometimes struggle with hanging processes that does not end, external programs launched within threads that I can’t reach anymore and so on.
Is there any “threading for dummies” python library out there? Thanks
Is there any “threading for dummies” python library out there?
No, there is not.
threadingis pretty simple to use in simple cases. You want to use it to introduce concurrency in your program. This means you want to use it whenever you want two or more actions to happen simultaneously, i.e. at the same time.This is how you can let Peter build a house and let Igor drive to Moskow at the same time:
Isn’t that simple? Define your function to be run in another thread. Create a
threading.Threadinstance with that function astarget. Nothing happend so far, until you invokestart. It fires off the thread and immediately returns.Before letting your main thread exit, you should wait for all the threads you have spawned to finish. This is what
t.join()does: it blocks and waits for the threadtto finish. Only then it returns.