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.
--
https://mail.python.org/mailman/listinfo/python-list