Queue and signals

skip at pobox.com skip at pobox.com
Tue Jan 16 19:51:15 EST 2007


    >> Is it legal to use Queue.put in a signal handle and Queue.get in the
    >> main process (no thread)

    hg> PS: and if not is there a way to share data between a "main" and its
    hg> signal in a safe way (write in signal and read in main)

The Queue module was designed to work in multithreaded contexts.  I doubt it
will work as expected in a nonthreaded application.  Given that your
application isn't multithreaded I suspect you can just modify an object that
both your signal handler and the rest of the application reference.  In
particular, there's no reason your signal handler can't be a method of an
instance:

    import signal
    import time

    class MyClass:
        def __init__(self):
            self.alarm_rang = False
            signal.signal(signal.SIGALRM, self.ring_alarm)
            signal.alarm(2)

        def ring_alarm(self, sig, frame):
            self.alarm_rang = True

    obj = MyClass()
    print obj.alarm_rang
    time.sleep(4.0)
    print obj.alarm_rang

Skip



More information about the Python-list mailing list