我正在查看Python文档中的signal和示例代码:
def handler(signum, frame):
print 'Signal handler called with signal', signum
raise IOError("Couldn't open device!")
Run Code Online (Sandbox Code Playgroud)
'frame'参数未在实际函数中使用.我在stackoverflow/online上关于信号处理程序的很多代码中注意到了这一点.什么是'frame'参数?为什么它保存在函数头中?
谢谢
小智 11
frame参数是堆栈帧,也称为执行帧.它指向被信号中断的帧.该参数是必需的,因为任何线程都可能被信号中断,但信号仅在主线程中接收.
例:
import signal
import os
import traceback
def handler(signum, frame):
print signum, frame
print "print stack frames:"
traceback.print_stack(frame)
def demo(n):
if n == 3:
os.kill(os.getpid(), signal.SIGUSR1)
return
demo(n+1)
signal.signal(signal.SIGUSR1, handler)
demo(1)
Run Code Online (Sandbox Code Playgroud)
输出:
$ python t.py
10 <frame object at 0x1e00520>
print stack frames:
File "t.py", line 17, in <module>
demo(1)
File "t.py", line 14, in demo
demo(n+1)
File "t.py", line 14, in demo
demo(n+1)
File "t.py", line 12, in demo
os.kill(os.getpid(), signal.SIGUSR1)
Run Code Online (Sandbox Code Playgroud)
frame 参数是Python 堆栈 frame。摘自手册:
使用两个参数调用处理程序:信号编号和当前堆栈帧(无或帧对象;有关帧对象的描述,请参阅类型层次结构中的描述或查看检查模块中的属性描述)。
它在您看到的示例中经常被忽略,因为它在 Python 调试器之外并不是特别有用。信号是异步的,并且会随意地命中进程。如果我将 SIGTERM 发送到您的进程并且您已经设置好处理它,那么您的代码在收到信号时所在的位置通常是无关紧要的。
| 归档时间: |
|
| 查看次数: |
6470 次 |
| 最近记录: |