thread behavior with join() and import

Tim Peters tim_one at email.msn.com
Tue May 9 02:55:23 EDT 2000


[eric jones]
> I'm curious as to why the following two threads behave differently.  When
> the "time" module is imported within the worker thread, a join() on that
> thread blocks indefinitely.  If however, the import outside of the worker
> thread, the join() occurs as I expected.
>
> Removing the join() on the second thread allows it to exit normally.  Why
> don't "import" and join() play well together, and is there a remedy?  I
> really need the import to occur within the worker thread because of some
> wxPython issues.
>
> I'm using 1.5.2.  The behavior is the same on both RH6.1 and NT 4.0.

Huh!  Works fine for me under Win95 -- get a real OS <wink>.

D:\Python>python misc/ttest.py
worker: without done
main: without  import done
main: with import done
worker: with done
worker: with done
main: with import done

D:\Python>

Are you running on a true multiprocessor?  I'm not.  Something to try:
create a global mutex and use it to serialize the import in with_import.run.
My bet is the problem will go away.

BTW, you'll probably have more luck redirecting this to the Thread-SIG (to
which I'm also sending this reply).

> import threading ,time
>
> class without_import(threading.Thread):
>     def run(self):
>         time.sleep(.5)
>         print 'worker: without done'
>
> class with_import(threading.Thread):
>     def run(self):
>         import time
>         time.sleep(.5)
>         print 'worker: with done'
>
> ############# this works fine
> wo = without_import()
> wo.start()
> wo.join()
> print 'main: without  import done'
>
> ######## without join(), thread also terminates normally
> w = with_import()
> w.start()
> print 'main: with import done'
>
> """
> ##### uncommenting this blocks indefinitely
> w = with_import()
> w.start()
> w.join()
> print 'main: with import done'
> """






More information about the Python-list mailing list