simple threading.Thread and Semaphores hang

L

lnenov

Hi,

My application hangs on exit.
I have isoleted this piece of code that reproduces the error: (the time
module is extra and not needed to reproduce)

import threading
import time

def func():
b = threading.Semaphore(value=0)
b.acquire()

a = threading.Thread(target=func)
a.start()

time.sleep(2)
quit()

When SystemExit is raised, nothing happens. Python hangs.

pdb does seem to have problem with this code too:
pdb smth.py
....
(Pdb) Traceback (most recent call last):
File "/usr/bin/pdb", line 1319, in <module>
pdb.main()
File "/usr/lib/python2.6/pdb.py", line 1312, in main
pdb.interaction(None, t)
File "/usr/lib/python2.6/pdb.py", line 198, in interaction
self.cmdloop()
File "/usr/lib/python2.6/cmd.py", line 130, in cmdloop
line = raw_input(self.prompt)
ValueError: I/O operation on closed file

Any help would be appreciated,
Lyudmil
 
A

Alain Ketterlin

lnenov said:
My application hangs on exit.
I have isoleted this piece of code that reproduces the error: (the
time module is extra and not needed to reproduce)

import threading
import time

def func():
b = threading.Semaphore(value=0)
b.acquire()

This waits for the semaphore to have a positive value. This thread is
blocked (apparently forever, since no other thread has access to the
semaphore).
a = threading.Thread(target=func)
a.start()

time.sleep(2)
quit()

When SystemExit is raised, nothing happens. Python hangs.

Your blocked thread is still around, still waiting for the semaphore.
Your program won't exit while there are non-daemon threads alive.

-- Alain.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top