Should PyImport_ImportModule be threadsafe when importing from zipfiles?

Geoff Bache geoff.bache at gmail.com
Thu Feb 20 04:11:08 EST 2020


Hi all,

I have some embedded Python code which looks like this in C++

_gstate = PyGILState_Ensure();
PyImport_ImportModule("a");
...
PyGILState_Release(_gstate);

and is called from different threads which are created in C++.

My module a.py then imports another module b in python, which defines a lot
of functions.

When several threads execute this simultaneously I often get a stacktrace
saying some function near the end of module b is not defined, presumably
because the module has been imported part-initialised.
This only seems to happen when my Python modules are packaged in a zip
file, not when they are ordinary files on disk.

I have observed this in both Python 3.7 and Python 3.8. Does anyone have
any insights or suggestions for how to debug this? It seems likely to be
hard to produce a reproducible test case.

Regards,
Geoff Bache


More information about the Python-list mailing list