simple threading.Thread and Semaphores hang

Alain Ketterlin alain at dpt-info.u-strasbg.fr
Mon Dec 6 08:15:04 EST 2010


lnenov <lnenov at mm-sol.com> writes:

> 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.



More information about the Python-list mailing list