I always had a small problem with threads and python. I never wanted anything “really” fancy, only a couple of lines that would allow me to send several requests at the same time. Thus, I never had the need of caring about writing to the same file, overwriting vars, etc. However, even such a simple example was complicated for me to 1) understand 2) find.
Today, thanks to my friend Miguel Pinheiro, I got not the technical knowledge, but one of those “a-ha!” insights. He also provided me with a _very_ clever script for multiprocessing tasks.
So, the most basic script I could write to test my threading abilities was a pinging script:
import os, time from threading import Thread class MyThread(Thread): def __init__(self, site): Thread.__init__(self) self.site = site def run(self): os.popen('ping -c 3 %s' %self.site) print "Pinged site %s" %self.site listofSites = ['www.google.pt', 'www.sapo.pt', 'www.portugal-a-programar.org', 'www.python.org'] print "Starting Single Thread Method" a= time.time() for site in listofSites: os.popen('ping -c 3 %s' %site) print "Pinged site %s" %site print "Took %s seconds" %str(time.time()-a) print "Starting Multithreaded Method" a= time.time() threads =  for site in listofSites: T = MyThread(site) threads.append(T) T.start() print "Thread Launched for %s" %site for i in threads: i.join() print "Took %s seconds" %str(time.time()-a)
The MyThread class gets arguments from init, as usual, but its the run method that is most important. It contains what we want to be ran when the thread is started. How do we start the thread? Call its start method. We can append threads to a list, like I did, and manage them to control how many we want to keep active (too many threads can overload the system) with a simple queueing system (maybe there’s something done already for it). Then, it comes join. Join basically works by making the script hang while the threads are running. It works automatically. But, if we need to wait for all the threads to be over before we actually do something (such as measuing the time they take to complete), we ought call it :)
The output of this script would be:
joao@wasp:~/Desktop$ python ping.py Starting Single Thread Method Pinged site www.google.pt Pinged site www.sapo.pt Pinged site www.portugal-a-programar.org Pinged site www.python.org Took 11.3715729713 seconds Starting Multithreaded Method Thread Launched for www.google.pt Thread Launched for www.sapo.pt Thread Launched for www.portugal-a-programar.org Thread Launched for www.python.org Pinged site www.python.org Pinged site www.google.pt Pinged site www.portugal-a-programar.org Pinged site www.sapo.pt Took 2.07188606262 seconds