I'm trying to understand OS concepts and Python libraries.
I came across a specific example mentioned in Python documentation https://docs.python.org/3/library/signal.html link which is not working for me on Windows.
import signal, os
def handler(signum, frame):
print('Signal handler called with signal', signum)
raise OSError("Couldn't open device!")
# Set the signal handler and a 5-second alarm
signal.signal(signal.SIGALRM, handler)
signal.alarm(5)
# This open() may hang indefinitely
fd = os.open('/dev/ttyS0', os.O_RDWR)
signal.alarm(0) # Disable the alarm
Is there any specific reason why singal.SIGALRM is not working on windows?
Auto complete is even showing the SIGALRM in Pycharm IDE (I'm assuming that there will be a variable or function if it shows like that).
But when I run the program, it is giving me the below error on Windows. I haven't checked this on Linux.
Traceback (most recent call last):
File "C:/Users/preddy53/Desktop/syst.py", line 8, in <module>
signal.signal(signal.SIGALRM, handler)
AttributeError: module 'signal' has no attribute 'SIGALRM'
Where am I doing wrong? Is it specific to operating system only?
Python signal handlers are always executed in the main Python thread of the main interpreter, even if the signal was received in another thread. This means that signals can't be used as a means of inter-thread communication. You can use the synchronization primitives from the threading module instead.
In simplistic terms, a signal is an event. A signal is used to interrupt the execution of a running function. The signals are always executed in the main Python thread. An event is generated to notify other parts of an application.
Try calling signal. alarm(0) when you want to disable the alarm. In all likelyhood it just calls alarm() in libc, and the man alarm says that alarm(0) "... voids the current alarm and the signal SIGALRM will not be delivered."
SIGALRM is an asynchronous signal. The SIGALRM signal is raised when a time interval specified in a call to the alarm or alarmd function expires. Because SIGALRM is an asynchronous signal, the SAS/C library discovers the signal only when you call a function, when a function returns, or when you issue a call to sigchk .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With