Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'mrab': 0.05; 'output': 0.05; '"""': 0.07; '-*-': 0.07; 'debug': 0.07; 'utf-8': 0.07; 'coding:': 0.09; 'line:': 0.09; 'processing,': 0.09; 'subject:Why': 0.09; 'try:': 0.09; 'python': 0.11; 'def': 0.12; 'thread': 0.14; 'empty,': 0.16; 'empty.': 0.16; 'inputs': 0.16; 'outputs': 0.16; 'subject:threads': 0.16; 'true:': 0.16; 'subject: ?': 0.16; 'wrote:': 0.18; 'input': 0.22; 'example': 0.22; 'import': 0.22; 'putting': 0.22; 'print': 0.22; 'skip:l 30': 0.24; "i've": 0.25; 'script': 0.25; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'that.': 0.31; 'class': 0.32; 'another': 0.32; 'url:python': 0.33; 'fri,': 0.33; 'skip:# 10': 0.33; 'skip:_ 10': 0.34; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'url:listinfo': 0.36; 'doing': 0.36; "didn't": 0.36; 'thanks': 0.36; 'url:org': 0.36; 'list': 0.37; 'implement': 0.38; 'skip:o 20': 0.38; 'skip:& 10': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'url:mail': 0.40; 'break': 0.61; 'skip:t 30': 0.61; 'from:charset:utf-8': 0.61; "you're": 0.61; 'hang': 0.67; 'url:me': 0.69; 'is!': 0.84; 'died': 0.91; 'here."': 0.91; 'whereas': 0.91; 'reply,': 0.93; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=u2MgB0OccgJvUY6AbMO5sdUmsh/ysLp1byIiGALlS2M=; b=TZBouVqFPT22s+yhtXE5CvaI7U7XAmjtruaf+hlv24+nikq9SpppC7nijMkoiA21/7 OJYpIvJmBLO/s70//gZCuTo6rjwA6a6rymvU3r6RbJphqDtYywIlYpR7BXieoUkAtuvC 9g4lADBIsq79LFqaatxT6XIWNUeGffCc2bZ1yv1VWK9cwCWzNrqjQj97T1hqb8oxfOZa X3/l36YHuGs9XNIfFRv1qNQ5WBlJPMKdX91HmMSRc/Mc/fZ2HR0omBQm4KZUqIWTRAy7 sOWUSiz8IbZnEb24aEiRNfIAdKuit8dwOHqp6PTtmgOJo//JG4j9uvAINHDM0d4UN2oq Cq6Q== MIME-Version: 1.0 X-Received: by 10.194.23.73 with SMTP id k9mr13706333wjf.24.1380904873554; Fri, 04 Oct 2013 09:41:13 -0700 (PDT) In-Reply-To: <524DB6AA.4080704@mrabarnett.plus.com> References: <524DB6AA.4080704@mrabarnett.plus.com> Date: Sat, 5 Oct 2013 00:41:13 +0800 Subject: Re: Why didn't my threads exit correctly ? From: =?UTF-8?B?5p2O5rSb?= To: python-list@python.org Content-Type: multipart/alternative; boundary=047d7b5d439674f88804e7ecf86c X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 194 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380904880 news.xs4all.nl 15916 [2001:888:2000:d::a6]:51251 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:55484 --047d7b5d439674f88804e7ecf86c Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Thanks for your reply, MRAB. I've seen that the child thread has been stoped, it just died in the queue.If I want the queue be empty, I must use queue.get() to dequeue and clean it, but I didn't do that. I've implement the thread using List now, thanks again. On Fri, Oct 4, 2013 at 2:25 AM, MRAB wrote: > On 03/10/2013 18:37, =E6=9D=8E=E6=B4=9B wrote: > >> Hi list, >> I write an example script using threading as follow. >> It look like hang when the list l_ip is empty. And any suggestion with >> debug over the threading in Python ? >> >> 1 #!/usr/bin/env python >> 2 # -*- coding: utf-8 -*- >> 3 import re >> 4 import os >> 5 import threading >> 6 from Queue import Queue >> 7 from time import sleep >> 8 >> 9 l_ip =3D [] >> 10 l_result =3D [] >> 11 result =3D re.compile(r"[1-3] received") >> 12 >> 13 class ping(threading.Thread): >> 14 """ """ >> 15 def __init__(self, l_ip, l_result): >> 16 threading.Thread.__init__(**self) >> 17 self.l_ip =3D l_ip >> 18 #self.l_result =3D l_result >> 19 >> 20 def run(self): >> 21 """ """ >> 22 while True: >> 23 try: >> 24 ip =3D self.l_ip.pop() >> 25 except IndexError as e: >> 26 print e >> 27 break >> 28 ping_out =3D os.popen(''.join(['ping -q -c3 ',ip]), 'r'= ) >> 29 print 'Ping ip:%s' % ip >> 30 while True: >> 31 line =3D ping_out.readline() >> 32 if not line: break >> 33 if result.findall(line): >> 34 l_result.append(ip) >> 35 break >> 36 >> 37 queue =3D Queue() >> 38 >> 39 for i in range(1,110): >> 40 l_ip.append(''.join(['192.168.**1.', str(i)])) >> 41 for i in xrange(10): >> 42 t =3D ping(l_ip, l_result) >> 43 t.start() >> 44 queue.put(t) >> 45 queue.join() >> 46 print "Result will go here." >> 47 for i in l_result: >> 48 print 'IP %s is OK' % i >> >> queue.join() will block until the queue is empty, which is never is! > > You're putting the workers in the queue, whereas the normal way of > doing it is to put them into a list, the inputs into a queue, and the > outputs into another queue. The workers then 'get' from the input > queue, do some processing, and 'put' to the output queue. > > -- > https://mail.python.org/**mailman/listinfo/python-list > --=20 All the best! http://luolee.me --047d7b5d439674f88804e7ecf86c Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Thanks for your reply, MRAB.

I've se= en that the child thread has been stoped, it just died in the queue.If I wa= nt the queue be empty, I must use queue.get() to dequeue and clean it, but = I didn't do that.
I've implement the thread using List now, thanks again.


On Fri, Oct 4, 2013= at 2:25 AM, MRAB <python@mrabarnett.plus.com> wrot= e:
On 0= 3/10/2013 18:37, =E6=9D=8E=E6=B4=9B wrote:
Hi list,
I write an example script using threading as follow.
It look like hang when the list l_ip is empty. And any suggestion with
debug over the threading in Python ?

=C2=A0 =C2=A01 #!/usr/bin/env python
=C2=A0 =C2=A02 # -*- coding: utf-8 -*-
=C2=A0 =C2=A03 import re
=C2=A0 =C2=A04 import os
=C2=A0 =C2=A05 import threading
=C2=A0 =C2=A06 from Queue import Queue
=C2=A0 =C2=A07 from time import sleep
=C2=A0 =C2=A08
=C2=A0 =C2=A09 l_ip =3D []
=C2=A0 10 l_result =3D []
=C2=A0 11 result =3D re.compile(r"[1-3] received")
=C2=A0 12
=C2=A0 13 class ping(threading.Thread):
=C2=A0 14 =C2=A0 =C2=A0 """ """
=C2=A0 15 =C2=A0 =C2=A0 def __init__(self, l_ip, l_result):
=C2=A0 16 =C2=A0 =C2=A0 =C2=A0 =C2=A0 threading.Thread.__init__(self= )
=C2=A0 17 =C2=A0 =C2=A0 =C2=A0 =C2=A0 self.l_ip =3D l_ip
=C2=A0 18 =C2=A0 =C2=A0 =C2=A0 =C2=A0 #self.l_result =3D l_result
=C2=A0 19
=C2=A0 20 =C2=A0 =C2=A0 def run(self):
=C2=A0 21 =C2=A0 =C2=A0 =C2=A0 =C2=A0 """ """=
=C2=A0 22 =C2=A0 =C2=A0 =C2=A0 =C2=A0 while True:
=C2=A0 23 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 try:
=C2=A0 24 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ip =3D se= lf.l_ip.pop()
=C2=A0 25 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 except IndexError as e:=
=C2=A0 26 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 print e =C2=A0 27 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 break
=C2=A0 28 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ping_out =3D os.popen(&= #39;'.join(['ping -q -c3 ',ip]), 'r')
=C2=A0 29 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 print 'Ping ip:%s&#= 39; % ip
=C2=A0 30 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 while True:
=C2=A0 31 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 line =3D = ping_out.readline()
=C2=A0 32 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if not li= ne: break
=C2=A0 33 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if result= .findall(line):
=C2=A0 34 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 l_result.append(ip)
=C2=A0 35 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 break
=C2=A0 36
=C2=A0 37 queue =3D Queue()
=C2=A0 38
=C2=A0 39 for i in range(1,110):
=C2=A0 40 =C2=A0 =C2=A0 l_ip.append(''.join(['192.168.1.= ', str(i)]))
=C2=A0 41 for i in xrange(10):
=C2=A0 42 =C2=A0 =C2=A0 t =3D ping(l_ip, l_result)
=C2=A0 43 =C2=A0 =C2=A0 t.start()
=C2=A0 44 =C2=A0 =C2=A0 queue.put(t)
=C2=A0 45 queue.join()
=C2=A0 46 print "Result will go here."
=C2=A0 47 for i in l_result:
=C2=A0 48 =C2=A0 =C2=A0 print 'IP %s is OK' % i

queue.join() will block until the queue is empty, which is never is!

You're putting the workers in the queue, whereas the normal way of
doing it is to put them into a list, the inputs into a queue, and the
outputs into another queue. The workers then 'get' from the input queue, do some processing, and 'put' to the output queue.

--
https://mail.python.org/mailman/listinfo/python-list



--
All the best!

--047d7b5d439674f88804e7ecf86c--