python's threading has no "interrupt"?

Gandalf gandalf at geochemsource.com
Tue Dec 2 10:38:47 CET 2003


>
>
>>As far as I know python's threading module models after Java's.
>>However, I can't find something equivalent to Java's interrupt and
>>isInterrupted methods, along with InterruptedException.
>>"somethread.interrupt()" will wake somethread up when it's in
>>sleeping/waiting state.
>>
>>Is there any way of doing this with python's thread? I suppose thread
>>interrupt is a very primitive functionality for stopping a blocked
>>thread.
>>    
>>
>Well, I haven't got any answer since I posted it. Meanwhile, I have
>been searching for it myself. Something new has been added in 2.3 in
>thread module. That's interrupt_main. But, unfortunately, it is the
>opposite of what I expected; It interrupts the main thread.
>
>After all this, I am a bit disappointed about Python. (it's sad)
>  
>
Did you try condition objects?   threading.Condition

Example from the library reference:

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()


You can block a thread with a condition object by calling its 'wait()' 
method. You can
call the 'notify()' method from another thread and that will 'interrupt' 
the blocked thread.

It is not the very same thing, but I suspect you can use it for the same 
purposes.

Cheers,

   Laci 1.0








More information about the Python-list mailing list