开发者

Python signal: reading return from signal handler function

开发者 https://www.devze.com 2023-03-09 16:06 出处:网络
Simple question; How do you read the return value of a function that is called as a signal handler? import signal

Simple question; How do you read the return value of a function that is called as a signal handler?

import signal

def SigHand(sig, frm):
    return 'SomeValue'

signal.signal(signal.SIGCHLD, SigHand)
signal.pause()

Is there a way to read the return value 'SomeValue' other than setting 开发者_JS百科it as a global?


You could create a simple class with a return value attribute.

>>> import signal
>>> class SignalHandler(object):
...     def __init__(self):
...         self.retval = None
...     def handle(self, sig, frm):
...         self.retval = sig
... 
>>> s = SignalHandler()
>>> s.retval
>>> signal.signal(signal.SIGALRM, s.handle)
0
>>> signal.alarm(1)
0
>>> s.retval
14
0

精彩评论

暂无评论...
验证码 换一张
取 消