Skip to content

Instantly share code, notes, and snippets.

@glarizza
Created January 6, 2011 15:41
Show Gist options
  • Save glarizza/768031 to your computer and use it in GitHub Desktop.
Save glarizza/768031 to your computer and use it in GitHub Desktop.
from threading import Thread
import subprocess
from Queue import Queue
num_threads = 4
queue = Queue()
ips = ["odm.huronhs.com", "10.13.2.1"]
#wraps system ping command
def pinger(i, q):
"""Pings subnet"""
while True:
ip = q.get()
print "Thread %s: Pinging %s" % (i, ip)
ret = subprocess.call("ping -c 1 %s" % ip,
shell=True,
stdout=open('/dev/null', 'w'),
stderr=subprocess.STDOUT)
if ret == 0:
print "%s: is alive" % ip
else:
print "%s: did not respond" % ip
q.task_done()
#Spawn thread pool
for i in range(num_threads):
worker = Thread(target=pinger, args=(i, queue))
worker.setDaemon(True)
worker.start()
#Place work in queue
for ip in ips:
queue.put(ip)
#Wait until worker threads are done to exit
queue.join()
@daknuett
Copy link

daknuett commented Mar 8, 2016

I think using threading.Pool would be a bit more readable&smaller.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment