[Python-Dev] Deadlock by a second import in a thread
brett at python.org
Sat Oct 20 20:57:28 CEST 2007
On 10/20/07, Facundo Batista <facundobatista at gmail.com> wrote:
> 2007/10/19, Christian Heimes <lists at cheimes.de>:
> > I know a possible solution. You could write a patch that moves the
> > imports in C code to the module init function and stores the modules in
> > a global static variable.
> I thought about this. It even will have the good side efect of not
> shooting the whole "import" machinery to see that you already imported
> it, every time you do an strptime.
> One question: The program makes this:
> PyObject *strptime_module = PyImport_ImportModule("_strptime");
> If I'd import it in the beggining of the file with the following...
> static PyObject *strptime_module = PyImport_ImportModule("_strptime");
> ... I'd never decref it, right?
Right. Otherwise, if the module is removed from sys.modules then it
will have a refcount of 0 and be collected, leaving your static
variable holding junk memory.
One issue with this approach, though, is that the import is a one-time
thing, and so replacing what is in sys.modules['_strptime'] will not
take affect in the module ever thanks to the caching of the module's
Granted this is a small issue as normal Python code does not pick up
changes in sys.modules, but it is something to be aware of.
More information about the Python-Dev