Python 中的信号处理程序是可重入的吗?
我有一个定时器的信号处理程序,它每秒对堆栈进行多次快照 - 它是一个统计分析器。如果需要太长时间,我的信号处理程序可以重新输入吗?如果是这样,我该如何保护它?
我的代码:
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)