Problems with imports on multiple threads, with embedded Python

dieter dieter at handshake.de
Fri Jan 5 01:52:25 EST 2018


geoff.bache at gmail.com writes:

> I have a multithreaded application using an embedded Python 3.6.4 (upgraded from 3.6.2 today in the hope that the problem was now solved: it doesn't seem to be). The standard library is in a zip file. So long as only one thread is running Python at a time it seems to work fine. But there seems to be a problem with the module importing when several Python threads are active.
>
> I get a variety of errors indeterministically, usually indicating that some symbol hasn't been imported. This occurs both in my own code and in the standard library. The most frequent is probably this line:

I do not know your specific problem (up to now, I used only Python 2),
but I know about import problems in multi threaded applications.
The problem is easily understood: import affects the global 
object "sys.modules". To protect this object, Python must do something.
In former Python versions (somewhere before Python 3), it was using a 
a thread lock: this could cause a deadlock in the case of some import
dependencies. The recoomendation to avoid the problem has been to
avoid module level imports for modules imported by threads.

Python 3 may have changed the way to protect "sys.modules".


In case of recursive import dependencies, you may see that some symbols are not
defined even in single threaded applications -- however then usually
deterministically.


At your place, I would start to check whether your application
has recursive import dependancies -- and in this case, remove them.




More information about the Python-list mailing list