need something like threads, or just multiple simulateous exec's

Josiah Carlson jcarlson at nospam.uci.edu
Mon Jan 26 10:14:27 EST 2004


Serge A. Ribalchenko wrote:
> Hi all,
> 
> I'm new in python coding...
> Can someone point me how can I do such a usual thing? I need to ping
> some hosts, and harvest results in one list. Just cause there is about 
> 120 hosts in the LAN, and approx. 10% is down at working hours, pinging
> each host at a time from cycle is VERY slow solution.
> I did:
> retv = os.popen('ping -c 1 ' + ip + ' |grep \"1 packet\" |awk  \'{print 
> $4}\'')
> res = retv.read()
> In cycle. BUT. I need something FASTER. And I feel fear to scary word 
> 'thread'. May someone give me some examle using threads for this 
> purpose? Thanks.
> 
> 
> Best wishes,
> ~       Serge.
> 

import threading
import Queue
import os

output = Queue.Queue()

def f(ip, output=output):
     output.put(os.popen('ping -c 1 ' + ip + ' |grep \"1 packet\" \ 
|awk\'{print > $4}\'')


for ip in host:
     threading.Thread(target=f, args=(ip,)).start()

for ip in host:
     print output.get()


  - Josiah



More information about the Python-list mailing list