简体   繁体   English

Python信号处理程序和重新进入

[英]Python signal handlers and reentry

Are signal handlers in Python reentrant? Python中的信号处理程序是否可以重入?

I have a signal handler for a timer that snapshots the stack many times a second - its a statistical profiler. 我有一个定时器的信号处理程序,它可以每秒多次快照堆栈 - 它是一个统计分析器。 Can my signal handler re-enter if it takes too long? 如果花费太长时间,我的信号处理程序可以重新进入吗? If so, how can I guard it? 如果是这样,我该怎样保护呢?

My code : 我的代码

import signal, time, traceback, threading

def start(interval=0.1):
    global _interval, _samples
    _samples = []
    signal.signal(signal.SIGALRM,_sample)
    signal.setitimer(signal.ITIMER_REAL,interval,interval)

def stop():
    global _samples
    signal.setitimer(signal.ITIMER_REAL,0,0)
    samples, _samples = _samples, []
    samples.append((time.time(),None,None,[]))
    return samples

def _sample(signo,frame):
    thread = threading.current_thread()
    row = (time.time(),thread.ident,thread.name,traceback.extract_stack(frame))
    if not _samples or row[1:] != _samples[-1][1:]: # new stack since last sample?
        _samples.append(row)

In general there is no way to block a signal in python. 一般来说,没有办法阻止python中的信号。 However, SIGALRM and friends cannot be stacked, so only one will be sent at a time. 但是,SIGALRM和朋友无法堆叠,因此一次只能发送一个。 If you simply schedule the next timer event at the end of your handler function, you will not have to worry about reentrancy. 如果您只是在处理函数结束时安排下一个计时器事件,则不必担心重入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM