Why didn't my threads exit correctly ?

李洛 luolee.me at gmail.com
Thu Oct 3 13:37:20 EDT 2013


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

-- 
All the best!

http://luolee.me
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20131004/7b1fed6a/attachment.html>


More information about the Python-list mailing list