I came across this answered post that seems to indicate closures installed as signal handlers should capture their environment, however I'm not seeing that (using Python 2.7). For example:
import os
import signal
import time
if __name__ == "__main__":
pid = os.fork()
if pid:
signal.pause()
else:
sigint = False
def handler(s, f):
# global sigint
print "sigint: {}".format(sigint)
print "Signal handler"
sigint = True
signal.signal(signal.SIGINT, handler)
signal.pause()
if sigint:
print "Caught sigint, sleeping briefly"
time.sleep(2)
print "exiting..."
Which running and triggering with ^C raises UnboundedLocalError: local variable 'sigint' referenced before assignment in the child. Uncommenting the global declaration fixes this. Clearly this is at odds with the aforementioned post, is the previous post simply incorrect, or am I doing something wrong here?
EDIT: Also the forking here is entirely extraneous, I was intending to investigate a different behavior when I noticed this one and just took the little script verbatim. The else block can replace the entire main thread to achieve the same result.