Queue and signals

H

hg

Hi,

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

Thanks,

hg
 
H

hg

hg said:
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
 
H

hg

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
 
S

skip

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top