On 10/19/07, Facundo Batista <[EMAIL PROTECTED]> wrote: > Hi! > > I was looking to this bug: http://bugs.python.org/issue1255 > > It basically creates a deadlock in Python by doing the following: > > - aa.py imports bb.py > - bb.py imports time and generates a thread > - the thread uses time.strptime > > The deadlock is because the strptime function imports another module, > line 517 of timemodule.c: > > PyObject *strptime_module = PyImport_ImportModule("_strptime"); > > This situation is well known, found a lot of references to this > import-thread-import problem in discussions and previous bugs (i.e.: > http://bugs.python.org/issue683658). > > What I did *not* find, and why I'm asking here, is how to solve it. > > Exists a known solution to this?
When python encounters a recursive import within a single thread it allows you to get access to partially-imported modules, making the assumption that you won't do any significant work until the entire import process completes. Only one thread is allowed to do an import at a time though, as they'll do significant work with it immediately, so being partially-imported would be a race condition. Writing a python file as a script means you do significant work in the body, rather than in a function called after importing completes. Importing this python file then violates the assumption for single-threaded recursive imports, and creating threads then violates their safety assumptions. The solution then is, if your python file will ever be imported, you must write a main function and do all the work there instead. Do not write it in the style of a script (with significant work in the global scope.) -- Adam Olsen, aka Rhamphoryncus _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com