- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我稍微修改了 official docs 中的信号示例(页面底部)。
我正在调用 sleep 10
但我希望在 1 秒后发出警报。当我运行以下代码片段时,触发异常的时间超过 1 秒(我认为它运行了整整 10 秒)。
import signal, os
def handler(signum, frame):
print 'Interrupted', signum
raise IOError("Should after 1 second")
signal.signal(signal.SIGALRM, handler)
signal.alarm(1)
os.system('sleep 10')
signal.alarm(0)
在单线程应用程序中,如何确保在超时后终止函数?
最佳答案
来自 the docs :
A Python signal handler does not get executed inside the low-level (C) signal handler. Instead, the low-level signal handler sets a flag which tells the virtual machine to execute the corresponding Python signal handler at a later point(for example at the next bytecode instruction).
因此,在某些情况下,诸如 signal.alarm()
生成的信号在超时后无法终止函数。 the function should cooperate by allowing other Python code to run (e.g., by calling PyErr_CheckSignals()
periodically in C code)或者你应该 use a separate process, to terminate the function in time .
如果您使用 subprocess.check_call('sleep 10'.split())
而不是 os.system('sleep 10')
,您的情况就可以得到解决。
关于python - signal.alarm 没有按时触发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34878618/
来自Unix&Linux Stack Exchange的 This question was migrated,因为可以在Stack Overflow上进行回答。
所以我有这段代码(部分取自 python 文档): import signal def handler(signum, frame): print 'Signal handler called
是否可以在信号块中调用用户定义的方法? method my-method ( ... ) { signal(SIGTERM,SIGINT,SIGQUIT,SIGHUP).tap( -> $si
我正在研究一个类的概念性伪代码信号量分配。 我想知道是否可以在某个进程调用 wait() 之前在信号量上调用 signal()。例如: Shared data: Semaphore x = 0; Pr
我正在为远程调试注册一个信号: signal.signal(signal.SIGUSR1, lambda x,y: remote_debug(x, y, emp_id)) 虽然通常非常快,但日志语句显
关于 PySide signals and slots page它说:“信号是实例拥有的运行时对象,它们不是类属性”。显然,QObject 构造函数在 Signals 的类属性中查找并将它们复制到对象
关于 PySide signals and slots page它说:“信号是实例拥有的运行时对象,它们不是类属性”。显然, QObject 构造函数查找 Signals 的类属性并将它们复制到对象实
关于PySide signals and slots page它说:“信号是实例拥有的运行时对象,它们不是类属性”。显然,QObject 构造函数在类属性中查找信号并将它们复制到对象实例。我的测试程序
使用 ReactiveCocoa,我如何从重复信号中链接信号? 我想做这样的事情:每 5 秒,我运行一次网络请求。 为此,我创建了一个重复信号 RACSignal *each5SecondSignal
正如标题中所写 - “信号和插槽”是一种简单的响应式(Reactive)编程方式吗? 最佳答案 Signals/Slots是 Observer Pattern 的实现. 来自维基 Reactive P
您好,我想知道信号声明如何在 VHDL 中真正起作用。它是否意味着延迟,因为它是内部信号?信号有内部存储器吗? 例子: Architecture SD_BEH of SD is signal C: s
我正在尝试编写一些代码来使用 python 执行一些数据包嗅探 使用 pyshark.I 有以下一段代码: import pyshark print('Pyshark demo') capture =
**披露 - 我是一名初学者 C 程序员,并不完全熟悉如何解释手册页,也不完全熟悉谈论 C 时正确的单词选择 - 对这个问题的任何编辑或澄清都是感谢。* 问题:我正在阅读有关 signal(7) 的手
我的应用程序运行完美,没有任何问题。但是当我尝试调试它时,android studio 卡住了。所以我收到消息“等待调试器”,然后该消息消失,接下来我在模拟器中只看到黑屏。 我也收到了消息 SIGNA
我在 Laravel 5.7 中使用 Snappy PDF 库。在本地一切正常,但在我的开发环境中出现此错误:该进程已收到信号“11”的信号。到现在为止,我找不到可以帮助我的解决方案。这是我在 Con
当我想在 linux 上激活 mongo 时,我得到了这个错误,它不会工作 $ sudo systemctl status mongod ● mongod.service - MongoDB 数据库服
我正在尝试设置函数超时,但我无法成功。 我运行来自 https://docs.python.org/3/library/signal.html?highlight=signal%20sigalrm#e
我在尝试使用 Phantomjs 和 Symfony 的 Process 和 Reposonse 文件创建 PDF 文档时遇到此错误。 这是我收到的错误信息 fatal error :未捕获的异常 '
我正在尝试调试一个复杂的 Perl 应用程序,该应用程序以错误消息“收到信号 SIGCHLD,但未设置信号处理程序”而终止。我知道它来自 Perl 解释器本身,特别是来自文件 mg.c它不能被捕获。但
我有以下代码,它按预期工作: import signal def printer(signum, frame): print("hi!") signal.signal(signal.SIGAL
我是一名优秀的程序员,十分优秀!