Why is signal.SIGALRM not working in Python on windows?
Is there any specific reason why singal.SIGALRM is not working on windows?
Yes, Windows OS doesn't implement that signal. The example you found starts with:
Here is a minimal example program. It uses the
alarm()
function to limit the time spent waiting to open a file; [...]
and the signal.alarm()
function is documented as:
Availability: Unix.
Next, the SIG*
section elsewhere on the module documentation page states:
Note that not all systems define the same set of signal names; only those names defined by the system are defined by this module.
So SIGALRM
is not available on Windows so you get an attribute error instead.
Note that Windows also does not have a /dev
virtual filesystem, so the os.open('/dev/ttyS0', os.O_RDWR)
call would fail too.
See python: windows equivalent of SIGALRM for an alternative using threads.
python: windows equivalent of SIGALRM
It's not very pretty, but I had to do something similar in a cross-platform way, and I came up with using a separate thread. Signal based systems did not work on all platforms reliably.
Use of this class could be wrapped up in a decorator, or made into a with
context handler.
YMMV.
#!/usr/bin/env python2.7
import time, threading
class Ticker(threading.Thread):
"""A very simple thread that merely blocks for :attr:`interval` and sets a
:class:`threading.Event` when the :attr:`interval` has elapsed. It then waits
for the caller to unset this event before looping again.
Example use::
t = Ticker(1.0) # make a ticker
t.start() # start the ticker in a new thread
try:
while t.evt.wait(): # hang out til the time has elapsed
t.evt.clear() # tell the ticker to loop again
print time.time(), "FIRING!"
except:
t.stop() # tell the thread to stop
t.join() # wait til the thread actually dies
"""
# SIGALRM based timing proved to be unreliable on various python installs,
# so we use a simple thread that blocks on sleep and sets a threading.Event
# when the timer expires, it does this forever.
def __init__(self, interval):
super(Ticker, self).__init__()
self.interval = interval
self.evt = threading.Event()
self.evt.clear()
self.should_run = threading.Event()
self.should_run.set()
def stop(self):
"""Stop the this thread. You probably want to call :meth:`join` immediately
afterwards
"""
self.should_run.clear()
def consume(self):
was_set = self.evt.is_set()
if was_set:
self.evt.clear()
return was_set
def run(self):
"""The internal main method of this thread. Block for :attr:`interval`
seconds before setting :attr:`Ticker.evt`
.. warning::
Do not call this directly! Instead call :meth:`start`.
"""
while self.should_run.is_set():
time.sleep(self.interval)
self.evt.set()
Python Standard Lib, signal :: AttributeError: module 'signal' has no attribute 'SIGALRM'
SIGALRM is not supported on Windows.
https://docs.python.org/2/library/signal.html
On Windows, signal() can only be called with SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, or SIGTERM. A ValueError will be raised in any other case
How to implement signal handling?
Notice that signal.SIGALRM
is only available on Unix.
Since you are using a Windows machine notice what is stated on the signal
documentation:
On Windows,
signal()
can only be called withSIGABRT
,SIGFPE
,SIGILL
,SIGINT
,SIGSEGV
,SIGTERM
, orSIGBREAK
. AValueError
will be raised in any other case. Note that not all systems define the same set of signal names; anAttributeError
will be raised if a signal name is not defined asSIG*
module level constant.
Related Topics
Opening Filenames with Colon (":") in Windows 7
Python Program with Notification in Gnome Shell Doesn't Work
Run Python Script Only If It's Not Running
How to Show Pil Image in Ipython Notebook
Fastest Way to Get System Uptime in Python in Linux
Reading from Linux Command Line with Python
How to Get a List of Locally Installed Python Modules
No Schema Has Been Selected to Create in ... Error
How to Sudo the Current Process
Faster Way to Find Large Files with Python
How to Get Each Dependent Command Execution Output Using Paramiko Exec_Command
Python 2.7 Cannot Import Pyqt4
How Can Python Handle Systemctl Stop
Histogram of an Image's "Black Ink Level" by Horizontal Axis