【发布时间】:2019-03-17 17:00:00
【问题描述】:
我正在尝试了解操作系统概念和 Python 库。
我遇到了 Python 文档 https://docs.python.org/3/library/signal.html 链接中提到的一个特定示例,该示例在 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
singal.SIGALRM 在 Windows 上不起作用有什么具体原因吗?
自动完成甚至在 Pycharm IDE 中显示 SIGALRM(我假设如果显示这样的话,会有一个变量或函数)。
但是当我运行程序时,它在 Windows 上给了我以下错误。我还没有在 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'
我哪里做错了?它仅特定于操作系统吗?
【问题讨论】:
-
因为Windows没有这样的信号。
-
因为它不会有
/dev/ttyS0。
标签: python linux windows python-3.x signals