Why does this hang sometimes?

Jason Friedman jason at powerpull.net
Sat Apr 7 18:54:28 EDT 2012


> I am just playing around with threading and subprocess and found that
> the following program will hang up and never terminate every now and
> again.
>
> import threading
> import subprocess
> import time
>
> def targ():
>    p = subprocess.Popen(["/bin/sleep", "2"])
>    while p.poll() is None:
>        time.sleep(1)
>
> t1 = threading.Thread(target=targ)
> t2 = threading.Thread(target=targ)
> t1.start()
> t2.start()
>
> t1.join()
> t2.join()
>
>
> I found this bug, and while it sounds similar it seems that it was
> closed during python 2.5 (I'm using 2.7.2):
> http://bugs.python.org/issue1404925

I can confirm hanging on my installation of 2.7.2.  I also ran this
code 100 times on 3.2.2 without experiencing a hang.  Is version 3.x a
possibility for you?



More information about the Python-list mailing list