Prev: Wing IDE 3.2.8 released: Adds Python 2.7 support
Next: How to generate execute file that include enthought.traits.api, enthought.traits.ui.api ?
From: MRAB on 4 Jun 2010 10:52
> Hi there,
> That's another try to get help about this issue I am facing. To help you
> to help me here goes a simple example. This is a very simplification of
> a very complex code.
> -------------------- thread_ping.py -------------------- begin
> import time
> import sys
> import atexit
> from threading import Thread
> class testit(Thread):
> def __init__ (self,ip):
> self.ip = ip
> self.status = 0
> def run(self):
> pingaling = os.popen("ping -q -c2 "+self.ip,"r")
> while 1:
> line = pingaling.readline()
> if not line: break
> igot = re.findall(testit.lifeline,line)
> if igot:
> self.status = int(igot)
> def goodbye(t, report):
> print 'Goodbye'
> print "Status from ",t.ip,"is",report[t.status]
> testit.lifeline = re.compile(r"(\d) packets received")
> report = ("No response","Partial Response","Alive")
> ip = "126.96.36.199" # www.google.com <http://www.google.com> # try
> with a failing IP if you want the test to last a bit longer
> t = testit(ip)
> atexit.register(goodbye, t, report)
> -------------------- thread_ping.py -------------------- end
> If one runs like:
> amadeus:~/TMP% time python2.6 thread_ping.py
> Status from 188.8.131.52 is Alive
> python2.6 thread_ping.py 0.02s user 0.02s system 3% cpu 1.056 total
> (i.e., python2.6 wait till the thread finishes and leave... I don't want
> this behaviour, see below)
> amadeus:~/TMP% time python2.5 thread_ping.py
> Status from 184.108.40.206 is No response
> python2.5 thread_ping.py 0.01s user 0.01s system 90% cpu 0.030 total
> (i.e., python2.5 promptly quit, as desired)
> Could someone tell me how to get python2.6 to have the same behaviour I
> see with python2.5 for this example code?
> Any hint would be very much appreciated indeed.
> Many thanks in advance.
The script won't exit while there are non-daemon threads running. You
can make a thread a daemon by setting its 'daemon' attribute to True
before starting the thread. If you want the script to be backwards
compatible with Python 2.5 then use the 'setDaemon' method instead.
I don't know why the script exited in Python 2.5 even though a
non-daemon thread was still running.