Search code examples
pythonsubprocessgevent

How to judge all the subprocess over in gevent?


I want to Traceroute some ip in a file with gevent, here is my code:

#!/usr/bin/env python
import gevent
from gevent import subprocess
gevent.monkey.patch_thread

ip_list = open('ips.txt')
data = [i for i in ip_list]
length = len(data)

process_pool = []

for i in range(length):
  process = subprocess.Popen(['sudo','traceroute','-T', '-m', '8', data[i]], stdout=subprocess.PIPE)
  process_pool.append(process)

print len(process_pool)

gevent.wait(process_pool, timeout=10)

count = 0

for i in process_pool:
  if i.poll() is not None:
    count += 1
    print i.stdout.read()
  else:
    print ('job is still running')

print count

it works well, but I don't know how to see if all the traceroute is over, if I change the value of timeout. All the process may over, but I don't know how many ip in the file, so the code can't deal with common case. How to fix it?


Solution

  • In fact, I just want to use gevent to see the speed of the code,so I can use the code like this, it fast enough for my job:

    #!/usr/bin/python
    
    import gevent
    from gevent import monkey, subprocess
    gevent.monkey.patch_thread
    
    all_data = []
    
    def my_trace(ip_addr):
      process = subprocess.Popen(['sudo','traceroute','-T','-m','8', ip_addr], stdout=subprocess.PIPE)
      all_data.append(process.stdout.read())
    
    ip_list = open('ips.txt')
    data = [i for i in ip_list]
    jobs = [gevent.spawn(my_trace, line) for line in data]
    gevent.wait(jobs)
    
    for i in all_data:
      print i
    
    print len(all_data)
    

    And, I just wait for all the traceroute end, that's all.