What functions, other than sleep(), can be interrupted by Ctrl-C?
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Thu Feb 26 15:47:13 EST 2009
En Thu, 26 Feb 2009 16:17:42 -0200, Maxim Khitrov <mkhitrov at gmail.com>
escribió:
> I'm looking for a function in the standard library or pywin32 package
> that will block until a certain condition is met or it is interrupted
> by Ctrl-C. For example, time.sleep() would have been perfect for my
> needs if thread.interrupt_main() could interrupt the call from another
> thread in the same way that Ctrl-C does. Unfortunately, that is not
> the case.
>
> Another thing I tried was creating a pipe with os.pipe() and issuing a
> read call on it. The event to exit was a single byte written to the
> other end of the pipe, but Ctrl-C could not interrupt the read call.
> The threading.Event class does not work for me, because it uses short
> sleep intervals for timed waits. I need the reaction to be as close to
> instant as possible, something like this will not do:
>
> while not <some condition is met>:
> sleep(0.01)
>
> I actually replaced threading.Event with my own version that uses
> native Windows events, and waits on those also cannot be interrupted
> by Ctrl-C. I'm trying to achieve the same effect as that while loop
> and I don't care what the condition to exit is, but the loop needs to
> exit as soon as the condition is met without waiting for up to X
> additional milliseconds. Any ideas?
You may try MsgWaitForMultipleObjects - send a message to the main thread
from the other thread.
An alertable wait (like SleepEx) plus QueueUserAPC should work, I presume,
but I've never actually tried in Python.
--
Gabriel Genellina
More information about the Python-list
mailing list