threading and atexit: different behaviour in python2.6 from 2.5
Alan
alanwilter at gmail.com
Mon Jun 7 04:43:01 EDT 2010
Thanks MRAB. setDaemon gave some hope.
Alan
On Fri, Jun 4, 2010 at 16:46, <python-list-request at python.org> wrote:
> Alan wrote:
>
>> 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):
>> Thread.__init__(self)
>> 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[0])
>>
>> 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 = "209.85.227.104" # 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)
>>
>> t.start()
>>
>> exit()
>> -------------------- thread_ping.py -------------------- end
>>
>> If one runs like:
>>
>> amadeus[2579]:~/TMP% time python2.6 thread_ping.py
>> Goodbye
>> Status from 209.85.227.104 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)
>>
>> and
>>
>> amadeus[2580]:~/TMP% time python2.5 thread_ping.py
>> Goodbye
>> Status from 209.85.227.104 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.
>
--
Alan Wilter S. da Silva, D.Sc. - CCPN Research Associate
Department of Biochemistry, University of Cambridge.
80 Tennis Court Road, Cambridge CB2 1GA, UK.
>>http://www.bio.cam.ac.uk/~awd28<<
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100607/8464b57b/attachment-0001.html>
More information about the Python-list
mailing list