Python Tip #4: Simple Threading

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) = site

    def run(self):
        os.popen('ping -c 3 %s'
        print "Pinged site %s"

listofSites = ['', '', '', '']

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)
    print "Thread Launched for %s" %site

for i in threads:

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
Starting Single Thread Method
Pinged site
Pinged site
Pinged site
Pinged site
Took 11.3715729713 seconds
Starting Multithreaded Method
Thread Launched for
Thread Launched for
Thread Launched for
Thread Launched for
Pinged site
Pinged site
Pinged site
Pinged site
Took 2.07188606262 seconds

One thought on “Python Tip #4: Simple Threading

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s