Python threading. Here are two, simple examples that get threading to work on python:

This is an example of a “fastping” style program using Pool and map:

#!/usr/bin/python2

from multiprocessing import Pool
import os,sys
import subprocess
import fileinput

devnull = open(os.devnull,'w')
threads = 128
ip_list = [ip.strip() for ip in fileinput.input()] #Reads files from argv[1:], or stdin if no argv present

def fast_ping(ip):
    ret_value = subprocess.Popen(['ping','-c 1','-W 1',ip],stdout=devnull).wait()
    if ret_value == 0:
        sys.stdout.write("\t{0}\n".format(ip)) #Print for threading
        return (0,ip)
    else:
        return (1,ip)

pool = Pool(threads) #I am going to have a 16-threaded application

print('Up:')

ip_status = pool.map(fast_ping,ip_list)
#This mapping is the secret sauce. You define the function, and the ITERABLE of arguments that will be provided to each thread.

print('Down:')
for ip in ip_status:
    if ip[0]:
        print('\t{0}'.format(ip[1]))

Here is the same program using threading. The advantage here is it is simpler to provide multiple arguments.

#!/usr/bin/python2

import os, subprocess, sys
import fileinput
from threading import Thread
from Queue import Queue

threads = 128
queue = Queue()
ip_status = []
ips = [ip.strip() for ip in fileinput.input()]
devnull = open(os.devnull,'w')

def fast_ping(ping_index,q):
    while True:
        ip = q.get()
        ret_value = subprocess.call(["ping","-c 1","-W 1",ip], stdout=devnull, stderr=devnull)
        if ret_value == 0:
            sys.stdout.write("\t{0}\n".format(ip))
            ip_status.append((0,ip))
        else:
            ip_status.append((1,ip))
        q.task_done()

print('Up:')

for index in range(threads):
    worker = Thread(target=fast_ping, args=(index, queue)) #Notice I pass two args here. Just to show it works
    worker.setDaemon(True)
    worker.start()

for ip in ips:
    queue.put(ip)

queue.join()

print('Down:')
for ip in ip_status:
    if ip[0]:
        print('\t{0}'.format(ip[1]))

Here’s sample output from both of them, demonstrating both usages (and that they do the same thing):

root@code-red:/com/testbed# time ./fastping_pool ips
Up:
        8.8.4.4
        8.8.8.8
Down:
        10.0.0.1
        1.2.3.4
        6.8.5.2
        4.4.4.4
        5.5.1.2
        65.196.35.165

real    0m1.285s
user    0m0.136s
sys     0m0.012s
root@code-red:/com/testbed# time cat ips | ./fastping_threading
Up:
        8.8.4.4
        8.8.8.8
Down:
        4.4.4.4
        5.5.1.2
        6.8.5.2
        10.0.0.1
        1.2.3.4
        65.196.35.165

real    0m1.042s
user    0m0.016s
sys     0m0.008s
Python – Threading and Reading from stdin

Leave a Reply

Your email address will not be published.