multithreading
Links
- http://stackoverflow.com/questions/10359985/is-in-python-thread-safe
- http://effbot.org/pyfaq/what-kinds-of-global-value-mutation-are-thread-safe.htm
- PyPy: STM update: back to threads?
- Threading and Import locks
- Forking and Threading
- From reddit comment
- If the application has more than one thread and
you can't control the "library" code that runs
in your new child, then the safest bet is to
prefork a child that'll fork new children on
your behalf.
- The multiprocessing module has support for this:
- start a new child prior to any threads being
started.
- When you want to start a new process,
ask that child to start the process on your
behalf.
- The child consisting of only a single
thread, forks and starts the process, then
(optionally) passes the new child's stdio pipe
fds back to the parent (this is the bit
multiprocessing has magic support for).
- At that point, your main process can safely
interact with the new cihld, requiring only the
exit status to be passed back via the proxy
child (since it is the new child's parent).
Gotchas
- Calling
.join()
from the main thread without a timeout
is uninterruptable. This means that signal handers
won't be invoked either. To work around it, add a
timeout to .join()
.