signal module?
Daniel Nielsen
djn at daimi.au.dk
Thu Mar 27 08:20:13 EST 2003
On 27/03-03 11.58, Michael Hudson wrote:
> Daniel Nielsen <djn at daimi.au.dk> writes:
>
> > I'm currently working on some code that uses the signal module... But
> > I doesnt seem to work proberly.
> >
> > I'm using threads,
>
> You are in trouble already.
That I am beginning to realize :)
> > so I'd like ctrl-c to actually terminate the program. If my memory
> > serves me, ctrl-c is SIGINT, so in my code, in the main thread, I
> > write:
> >
> > signal.signal(signal.SIGINT, gp.handler);
> >
> > where gp is a module and handler looks like:
> >
> > def handler(signum, frame):
> > print "Handling signal: " + signum;
> > sys.exit(2);
> >
> > But ctrl-c doesnt do anything!
> >
> > If I write:
> > signal.signal(signal.SIGINT, signal.SIG_DFL);
> > instead, ctrl-c actually terminates the program...
> > Have I misunderstood something?
>
> I'd expect this to work, but that it doesn't fails to surprise. What
> OS (+version, probably) are you on? Any chance of a complete code
> sample?
Well, I'm running it on the computers that belong to the University
where I study. They are heavily modified RH7.3. I have compiled and
installed python 2.2.2 as a local user.
I seem to have figured out what is going on:
1) When you use the signal module with your OWN handlers (e.g. not
signal.SIG_DFL or signal.SIG_IGN), it is required that the main
thread does not exit. The it works like a charm.
2) IF the mainthread exits, then then all handlers defined by signal
module is ignored. UNLESS it is signal.SIG_DFL, and signal.SIG_IGN.
So, I have solved the problem by not letting the main thread
exit... Seems funny though...
As for complete source, I have atttached the current version of the
two files. (it is a VERY work in progress, and I'm learning python
along the way). It's supposed to be a genetic programming project, and
I'm in charge of providing a skeleton distributed implementation.
Note, I'm using latest pyro from pyro.sourceforge.net (14. march)
/Daniel
--
There are no great men, only great challenges that ordinary men are forced
by circumstances to meet.
-- Admiral William Halsey
-------------- next part --------------
import threading, Pyro.naming, sys;
#A class, representing a individual
class Myre:
def __init__(self, name):
self.name=name;
def output(self):
print "Name: " + self.name;
class Population:
def __init__(self):
self.pop = [];
def __len__(self):
return len(self.pop);
def append(self, myre):
if isinstance(myre,Myre):
self.pop.append(myre);
else:
raise TypeError;
def output(self):
i = 0;
for m in self.pop:
print "Myre " + str(i) + ": ";
m.output();
i += 1;
class Server:
def __init__(self):
print "Bringing up server...";
def startNS(self):
print "Starting the nameserver..."
self.ns = NSstart();
#Making the application exit when onlye daemon threads are left
self.ns.setDaemon(True);
self.ns.start();
class NSstart(threading.Thread):
def __init__(self):
threading.Thread.__init__(self,name="ns");
def run(self):
self.ns = Pyro.naming.NameServerStarter();
self.ns.start();
#fkt for signal handling
def handler(signum, frame):
print "Handling signal:", signum;
sys.exit(2);
-------------- next part --------------
#!/usr/bin/env python
import os, sys, gp, getopt, signal;
CLIENTS=['meltdown','magneto','cyclops','blink'];
def nice():
#Set this proces nice value... eq. 10 - we are behaving!
current = os.nice(0);
inc = 10 - current;
print "Current niceness is " + str(current) + " adding "+ str(inc);
os.nice(inc);
def usage():
print "Usage: "+sys.argv[0]+" -c|--client -s|server";
def main():
nice();
try:
opt, args = getopt.getopt(sys.argv[1:],"cs",['client','server']);
except getopt.GetoptError:
usage();
sys.exit(2);
doClient = False;
doServer = False;
for o,a in opt:
if o in ("-c","--client"):
#Client time!
doClient = True;
if o in ("-s","--server"):
#Server Time!
doServer = True;
if doServer:
#getting around crtl+c and threads...
signal.signal(signal.SIGINT, gp.handler);
server = gp.Server();
server.startNS();
print "Creating population...";
myrerne = gp.Population();
myrerne.append(gp.Myre("FirkAnt"));
myrerne.append(gp.Myre("Legions"));
myrerne.append(gp.Myre("Militant"));
#ugly hack, not allowing main thread to exit. Should prob'ly sleep
while True:
pass;
elif doClient:
print "Doing client...";
#making script run... could be importet ya know...
if __name__ == "__main__":
main();
More information about the Python-list
mailing list