Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '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; 'empty,': 0.16; 'empty.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'inputs': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'outputs': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 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; 'header:User-Agent:1': 0.23; 'skip:l 30': 0.24; 'script': 0.25; 'header:In-Reply-To:1': 0.27; 'class': 0.32; 'another': 0.32; 'skip:# 10': 0.33; 'skip:_ 10': 0.34; 'except': 0.35; 'received:84': 0.35; 'doing': 0.36; 'list': 0.37; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'break': 0.61; 'skip:t 30': 0.61; "you're": 0.61; 'hang': 0.67; 'header:Reply- To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'is!': 0.84; 'reply-to:addr:python.org': 0.84; 'here."': 0.91; 'whereas': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=PIY2p5aC c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=LqG8EI-UCiYA:10 a=ziJoHlo4GSIA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=tMsJFA2S7AoA:10 a=LxUihAQLecvpN5IuXDAA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett:2500 Date: Thu, 03 Oct 2013 19:25:46 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Thunderbird/24.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Why didn't my threads exit correctly ? References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org 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: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380824734 news.xs4all.nl 15957 [2001:888:2000:d::a6]:34146 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:55435 On 03/10/2013 18:37, 李洛 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 = [] > 10 l_result = [] > 11 result = 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 = l_ip > 18 #self.l_result = l_result > 19 > 20 def run(self): > 21 """ """ > 22 while True: > 23 try: > 24 ip = self.l_ip.pop() > 25 except IndexError as e: > 26 print e > 27 break > 28 ping_out = os.popen(''.join(['ping -q -c3 ',ip]), 'r') > 29 print 'Ping ip:%s' % ip > 30 while True: > 31 line = ping_out.readline() > 32 if not line: break > 33 if result.findall(line): > 34 l_result.append(ip) > 35 break > 36 > 37 queue = 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 = 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.