summer_soccer
asked on
what's wrong with my code to do concurrent traceroute to multiple IP addresses?
I am new to Python. When debugging the python code, I got the following error messages:
Exception in thread Thread-49:
Traceback (most recent call last):
File "/usr/lib/python2.5/thread ing.py", line 486, in __bootstrap_inner
self.run()
File "/usr/lib/python2.5/thread ing.py", line 446, in run
self.__target(*self.__args , **self.__kwargs)
TypeError: traceroute_worker() argument after * must be a sequence
The code is below:
Can anyone help me figure out what is wrong with the code?
Exception in thread Thread-49:
Traceback (most recent call last):
File "/usr/lib/python2.5/thread
self.run()
File "/usr/lib/python2.5/thread
self.__target(*self.__args
TypeError: traceroute_worker() argument after * must be a sequence
The code is below:
Can anyone help me figure out what is wrong with the code?
def traceroute_worker(self, q):
proc, tracefile = q.get()
while True:
if proc.poll() is not None:
map_traceroute(tracefile)
q.task_done()
break
time.sleep(5)
def do_traceroute(self, peers):
sites = []
for p in peers:
sites.append(p[0][0])
print sites
q = Queue()
for i in range(50):
t = Thread(target=self.traceroute_worker, args=(q))
t.setDaemon(True)
t.start()
for item in sites:
out = open('some-file', 'w')
q.put(Popen(['/usr/bin/traceroute', item], stdout=out))
q.join() # block until all tasks are done
ASKER
Thanks ranrom.
I changed to args=(q,), but now I got the error message:
in traceroute_worker
proc, tracefile = q.get()
TypeError: 'Popen' object is not iterable
What is the cause of this error?
I changed to args=(q,), but now I got the error message:
in traceroute_worker
proc, tracefile = q.get()
TypeError: 'Popen' object is not iterable
What is the cause of this error?
ASKER
I changed code to below, and the error
TypeError: 'Popen' object is not iterable
is resolved.
But now I got another error:
in traceroute_worker
time.sleep(5)
AttributeError: 'builtin_function_or_metho d' object has no attribute 'sleep'
I did do "from time import time" in the beginning. Do anybody know how this error is generated and how to resolve it?
Many thanks!
TypeError: 'Popen' object is not iterable
is resolved.
But now I got another error:
in traceroute_worker
time.sleep(5)
AttributeError: 'builtin_function_or_metho
I did do "from time import time" in the beginning. Do anybody know how this error is generated and how to resolve it?
Many thanks!
def traceroute_worker(self, q):
proc = q.get()
tracefile = proc.stdout
while True:
if proc.poll() is not None:
map_traceroute(tracefile)
q.task_done()
break
time.sleep(5)
def do_traceroute(self, peers):
sites = []
for p in peers:
sites.append(p[0][0])
print sites
#t = SubnetTree.SubnetTree()
q = Queue()
for i in range(50):
t = Thread(target=self.traceroute_worker, args= (q, ))
t.setDaemon(True)
t.start()
for item in sites:
out = open('some-file', 'w')
q.put(Popen(['/usr/bin/traceroute', item], stdout=out))
q.join() # block until all tasks are done
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try:
t = Thread(target=self.tracero