Queue and signals

hg hg at nospam.org
Tue Jan 16 14:07:58 EST 2007


skip at pobox.com wrote:

> 
>     >> 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

Hi,

Well, so far it seems to work ...

The signal handler gets "called" by another application which in turn will
stuff info in my sys.stdin, I need then to "raw_input" the info and put it
in some type of data structure in a safe way so my main loop can then
reader/pop it.

ref: (ps: I'm trying to setup a Python high level interface for my kids to
learn the language / be interested) http://realtimebattle.sourceforge.net/

Regards,

hg





More information about the Python-list mailing list