为什么signal.SIGALRM在Windows的Python中不起作用?

编程入门 行业动态 更新时间:2024-10-11 05:28:31
本文介绍了为什么signal.SIGALRM在Windows的Python中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

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

I came across a specific example mentioned in Python documentation docs.python/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中不起作用?

本文发布于:2023-11-26 23:47:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635703.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   SIGALRM   signal   Python   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!