By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,932 Members | 1,944 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,932 IT Pros & Developers. It's quick & easy.

Queue and signals

P: n/a
hg
Hi,

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

Thanks,

hg

Jan 16 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
hg
hg wrote:
Hi,

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

Thanks,

hg

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

Thanks,

hg

Jan 16 '07 #2

P: n/a
>Is it legal to use Queue.put in a signal handle and Queue.get in the
main process (no thread)
hgPS: and if not is there a way to share data between a "main" and its
hgsignal 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
Jan 17 '07 #3

P: n/a
hg
sk**@pobox.com wrote:
>
>Is it legal to use Queue.put in a signal handle and Queue.get in
>the main process (no thread)

hgPS: and if not is there a way to share data between a "main" and
its hgsignal 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
Jan 17 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.