python: Windows 相当于 SIGALRM
我有这个装饰器:
def timed_out(timeout):
def decorate(f):
if not hasattr(signal, "SIGALRM"):
return f
def handler(signum, frame):
raise TimedOutExc()
@functools.wraps(f)
def new_f(*args, **kwargs):
old = signal.signal(signal.SIGALRM, handler)
signal.alarm(timeout)
try:
result = f(*args, **kwargs)
finally:
signal.signal(signal.SIGALRM, old)
signal.alarm(0)
return result
new_f.func_name = f.func_name
return new_f
return decorate
该代码只在 Linux 上执行任何操作,但与 Windows 上一样,没有 SIGALRM
。让这段代码在 Windows 中也能工作的最简单方法是什么?
I have this decorator:
def timed_out(timeout):
def decorate(f):
if not hasattr(signal, "SIGALRM"):
return f
def handler(signum, frame):
raise TimedOutExc()
@functools.wraps(f)
def new_f(*args, **kwargs):
old = signal.signal(signal.SIGALRM, handler)
signal.alarm(timeout)
try:
result = f(*args, **kwargs)
finally:
signal.signal(signal.SIGALRM, old)
signal.alarm(0)
return result
new_f.func_name = f.func_name
return new_f
return decorate
The code only does anything on linux, though, as on windows, there is no SIGALRM
. What would be the simplest way to have this code work in Windows as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它不是很漂亮,但我必须以跨平台的方式做类似的事情,并且我想出了使用单独的线程。基于信号的系统并不能在所有平台上可靠地工作。
此类的使用可以包装在装饰器中,或者制作成
with
上下文处理程序。YMMV。
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.
这个答案仅适用于 Cygwin..
...并且它可能不再适用于最新版本的 Cygwin...
我发现这个超时装饰器代码也非常方便。 (我最初在这个问题的答案中找到它:如何限制函数调用的执行时间?)
为了使其在 Windows 上运行,我使用随 Cygwin 一起安装的 Python。
我运行 setup-x86_64.exe,然后从 Python 文件夹中选择
python3
包。 (或者,如果您更喜欢 Python 2,则使用python
包。)定义了别名
为了将 python3 重命名为 python2,我从 Cygwin 命令提示符 。由于我不经常使用此功能,因此我可能不会将其放入 .bashrc 或其他任何内容中。
相关问题:
Python 信号甚至在 Cygwin 上也不起作用?
This answer works only in Cygwin..
... and it may no longer work with recent versions of Cygwin...
I find this timeout-decorator code very handy, too. (I originally found it in this question answer: How to limit execution time of a function call?)
To make it work on Windows, I use the Python that is installed with Cygwin.
I run setup-x86_64.exe, then select the
python3
package from the Python folder. (Or, if you prefer Python 2, thepython
package.)To rename python3 to python2, I define the alias
from the Cygwin command prompt. Since I don't use this functionality very often, I probably won't put it into a .bashrc or anything.
Related question:
Python signal don't work even on Cygwin?
解决原始问题:python:Windows 相当于 SIGALRM
有一种方法可以在 Windows 上创建 SIGALRM 类型事件。它利用多媒体定时器来触发事件。这是一个通用计时器,可以触发单个事件 (
periodic=False
)、多次重复 (n_reps >= 1
) 或通过外部停止连续触发事件(.stop()
函数调用)可以指定此计时器来生成间隔非常精确的事件,这些事件不依赖于 Python 运行时。此处,时间间隔以秒为单位指定(
间隔
和分辨率
)单个事件示例:
输出:
当前时间= 2024-03-24 22:42:16.475416
多个事件示例(然后停止):
输出:
给定时间后手动终止示例:
输出:
原始信用归于:
如何实现高速、一致的采样?
Addressing original question: python: windows equivalent of SIGALRM
There is a way to create a SIGALRM type event on Windows. It makes use of the multi-media timer to trigger the event. This is a general purpose timer which can trigger a single event (
periodic=False
), a number of repetitions (n_reps >= 1
), or continuous with an external stopping event (.stop()
function call)This timer can be specified to produce very accurately spaced events, which are not dependent on the Python runtime. Here, time intervals are specified in seconds (
interval
andresolution
)Example Single Event:
Output:
current time= 2024-03-24 22:42:16.475416
Example multiple events (then stop):
Output:
Example terminate manually after given time:
Output:
Original credit goes to:
How to implement high speed, consistent sampling?