473,320 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Re: Win32 trouble with threading, signals, and sleep()

Lowell Alleman wrote:
I'm running into this issue on Windows with the follow exception at
the time when the signal handler is called:

Traceback (most recent call last):
...
self.done.wait(30)
File "D:\Python24\lib\threading.py", line 348, in wait
self.__cond.wait(timeout)
File "D:\Python24\lib\threading.py", line 222, in wait
_sleep(delay)
IOError: [Errno 4] Interrupted function call

Well you've certainly picked a ticklish area to run
into problems with ;). First, forget about the
threading aspects for the moment. AFAICT the smallest
program which reproduces your problem is:

<code>
import signal
import time

def handler (*args):
pass

signal.signal(signal.SIGBREAK, handler)
time.sleep (10)

</code>

Now run that and do a ctrl-break somewhere in
that time.sleep. Sure enough...

<output>
Traceback (most recent call last):
File "C:\data\temp\sig3.py", line 8, in <module>
time.sleep (10)
IOError: [Errno 4] Interrupted function call

</output>

Under the covers, the sleep function is implemented
as a WaitForSingleObject call with a timeout. The
object being waited on is an anonymous event which
is set from a console ctrl handler when one of
those interrupts happens (ctrl-c / ctrl-break /
shutdown). This makes sure that the (otherwise
blocking) sleep can be interrupted. Something
like this:

<code>
import win32api
import win32event

hEvent = win32event.CreateEvent (None, 1, 0, None)
def handler (*args):
win32event.PulseEvent (hEvent)
return True

win32api.SetConsoleCtrlHandler (handler, 1)
#
# This line is basically the sleep happening
#
win32event.WaitForSingleObject (hEvent, 10000)

</code>

For reasons which I'm not aware of, the code
recognises that the interrupt has fired and
sets the WSAEINTR error code, which is the
IOError 4 which you're seeing, and then exits.
EINTR is usually issued when a blocking call
is cancelled explicitly, so presumably this
is considered a simulation of that. Not sure.

What happens now is that control passes
back out to the routine which called the
sleep. But... an exception condition has
been set inside the sleep call and is now
raised, appearing to come from within the
routine which called sleep.

Clear?

You could obviously continue to catch the
exception. Alternatively,
I think your best bet, as long as you can make
it work with the threads, is to install your
own console ctrl handler. Note that the docs
for this specify that:

"When the signal is received, the system creates
a new thread in the process to execute the function."

So you'll need to be quite careful to make your
code thread-safe. But it should work. I adapted
your example slightly to replace all of the lines
setting the signal handlers to one statement:

win32api.SetConsoleCtrlHandler (handler, 1)

and the handler function itself takes only one
arg, the signal no, and returns False to indicate
that control should pass to the next handler, which
will probably be the default Python handler:

def handler(arg):
print "Signal handler", arg
global workers
for w in workers:
w.stop()
return False
Hope that all helps.

TJG

Aug 9 '08 #1
0 1382

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Jane Austine | last post by:
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....
3
by: Peter Hansen | last post by:
I'm still trying to understand the behaviour that I'm seeing but I'm already pretty sure that it's either a bug, or something that would be considered a bug if it didn't perhaps avoid even worse...
5
by: Francois De Serres | last post by:
Hiho, could somebody please enlighten me about the mechanics of C callbacks to Python? My domain is more specifically callbacks from the win32 API, but I'm not sure that's where the problem...
2
by: rbt | last post by:
I have a win32 service written in Python. It works well. It sends a report of the status of the machine via email periodically. The one problem I have is this... while trying to send an email, the...
5
by: Lonewolf | last post by:
Hi all, I am not sure if it is even possible to do it from .NET itself. Is there something like a waitable timer which can resume the system from its hibernate or standby (S3: suspend to ram)...
17
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. ...
12
by: Perecli Manole | last post by:
I am having some strange thread synchronization problems that require me to better understand the intricacies of Monitor.Wait/Pulse. I have 3 threads. Thread 1 does a Monitor.Wait in a SyncLock...
3
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello Everyone, I am not sure what am I doing wrong here. I spend almost whole day working on this. I am trying to set focus on the process that I started by Process.start. Below is my code...
6
by: George Sakkis | last post by:
I'm baffled with a situation that involves: 1) an instance of some class that defines __del__, 2) a thread which is created, started and referenced by that instance, and 3) a weakref proxy to the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.