为什么signal.SIGALRM在Windows的Python中不起作用? [英] Why is signal.SIGALRM not working in Python on windows?

查看:1416
本文介绍了为什么signal.SIGALRM在Windows的Python中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解OS概念和Python库.

I'm trying to understand OS concepts and Python libraries.

我遇到了Python文档 https://docs.python中提到的特定示例. org/3/library/signal.html 链接在Windows上对我不起作用.

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

singal.SIGALRM在Windows上无法运行是否有任何特定原因?

Is there any specific reason why singal.SIGALRM is not working on windows?

自动完成甚至在Pycharm IDE中显示SIGALRM(我假设如果显示这样的变量或函数).

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).

但是,当我运行该程序时,它在Windows上给了我以下错误.我还没有在Linux上检查过.

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?

推荐答案

singal.SIGALRM在Windows上无法运行是否有任何特定原因?

Is there any specific reason why singal.SIGALRM is not working on windows?

是的,Windows操作系统没有实现该信号. 发现的示例以:

Yes, Windows OS doesn't implement that signal. The example you found starts with:

这是一个最小的示例程序.它使用alarm()函数来限制等待打开文件所花费的时间. [...]

Here is a minimal example program. It uses the alarm() function to limit the time spent waiting to open a file; [...]

signal.alarm()函数记录为:

and the signal.alarm() function is documented as:

可用性:Unix.

Availability: Unix.

接下来,模块文档页面上其他地方的SIG*部分指出:

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.

因此SIGALRM在Windows上不可用,因此会出现属性错误.

So SIGALRM is not available on Windows so you get an attribute error instead.

请注意,Windows也没有/dev虚拟文件系统,因此os.open('/dev/ttyS0', os.O_RDWR)调用也将失败.

Note that Windows also does not have a /dev virtual filesystem, so the os.open('/dev/ttyS0', os.O_RDWR) call would fail too.

有关使用线程的替代方法,请参见 python:等效于SIGALRM的Windows .

See python: windows equivalent of SIGALRM for an alternative using threads.

这篇关于为什么signal.SIGALRM在Windows的Python中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆