Web Hosting Philippines, Offshore Programming, Offshore SEO Philippines, Cheap Webhosting Manila Philippines
Home -> Resources -> Python -> Multithreading in Python - A tutorial and discussion of the thread versus threading module

About Multithreading - Pros and Cons

Multicore CPUs have been mainstream for sometime and the majority of notebooks and desktop machines that people purchase nowadays will have them inside. Thus, it is more important than ever for competent programmers to know how to take advantage of multithreading in their programs, or at least understand how threads work.

Threads are just one of the many abstractions that can take advantage of multiple execution units in the hardware. They are pretty low level and map pretty closely to how the hardware works. As such, they are in fact very tricky to use and debugging programs that use them require a degree of forethought and analysis on a completely different order from ordinary sequential programming. The following links are but two small samplings of comments on the issue and are provided for the reader to appreciate just how cautious one should be when considering the use of multithreading.

Even for 'experts', threads are fiendishly difficult to work with, despite their deceptive initial simplicity. I have had occasion to use threads in simple ways a few times in my programming career and while I thankfully have not gone through the sort of thread hell I've heard described, I could easily imagine situations where even the simple uses I have made of threading would have led to such. I chalk up the preservation of my virginity to being adequately forewarned and therefore curbing a great deal of ambitiousness in my use of threads.

In those instances where I chose to use threads, I did so because I felt that making the code multithreaded would lead to considerably simpler code structure (e.g. much fewer lines) than if I hadn't. The caveat was that if I wasn't careful, all the time I save making the code shorter and faster to read wouldn't make up for the time consumed figuring out where my code went wrong in the case of a horrible deadlock. My first use of threads was many years ago in Delphi to control a bill acceptor and after that a couple of times more in Python for network code. So far I have not encountered debugging nightmares and it seems to have been worth it to use multithreading.

For code tasks that can be very obviously distributed over multiple cpus/cores, you will definitely want to consider the use of multithreading. While anti-threaders will propose the use of many other alternative mechanisms - which you should definitely consider - the fact that all modern OSes support threads directly means that they are the most universally supported form of concurrency mechanism available. Thus you can reasonably expect a mature implementation to be present on just about any environment you may need to port your code to.

For people who intend to use multithreading to get the big speed gains promised by multicore machines, the big caveat is that under CPython, using threads will be useless for this purpose (see GIL). A similar limitation also applies for Ruby. Happily however, the GIL is not present under Ironpython nor Jython and so, running your multithreaded Python code under them WILL give you the expected performance benefits (see this post). Such instances of being able to have your cake and eat it too is one of the many reasons Python brings so much joy to its users.

How to use Threads in Python

Compare to other programming languages, the sample code to demonstrate how to use threads in Python is truly simple:

from sys    import stdout
from time   import sleep
from random import randint
from thread import start_new_thread

def worker(name):
  wait=randint(2,7)
  sleep(wait)
  print "I'm",name,"and waited",wait,"seconds."
  stdout.flush()

start_new_thread(worker,("thread 1",))
start_new_thread(worker,("thread 2",))
start_new_thread(worker,("thread 3",))

while True:
  pass

The above shows how to spawn threads using the older thread module. thread.start_new_thread() just takes a function worker() plus a tuple containing the arguments to be pass to it, and will then spawn that function in its own thread. Each thread, as you can see, executes independently of the others.

The Python threading Module

In addition to the thread module, Python has the newer threading module which is closely modeled after Java's Thread class and Runnable interface. You can use it the way Java threads are created - by creating a class that inherits from threading.Thread which contains your threaded code. Quite often though, this roundabout OO way of doing it is a gratuitous annoyance.

Especially at the beginning when you are still learning and simply do not need the added confusion and complexity, the far simpler way of creating threads using the threading module goes like this:

from sys       import stdout
from time      import sleep
from random    import randint
from threading import Thread

def worker(name):
  wait=randint(2,7)
  sleep(wait)
  print "I'm",name,"and waited",wait,"seconds."
  stdout.flush()

t1=Thread(target=worker,args=("thread 1",))
t2=Thread(target=worker,args=("thread 2",))
t3=Thread(target=worker,args=("thread 3",))

t1.start()
t2.start()
t3.start()

The nice thing here is that the while True loop is no longer required. Unlike the case with using thread.start_new_thread(), the interpreter will wait until all threads are finished before exiting. The threading module contains a lot of extra functionality you may eventually need as your multithreaded programs become more complex, so you should be aware of its existence and what it brings to the table.

 Want to tip via bitcoin?

(address string below for double checking)
 1KDgw4y5AUSUhmRVQSWiVS43YvrrxLmc8u

Back to Top

© 2017 by Andy Sy
last updated: 2010-Apr-11

NeoTitans- Offshore Outsourced Corporate Web and Software Development Internet Marketing SEO

Web Development / Rich Internet Applications (RIA) Development

Programming Languages

Platforms

Database Development




Web Hosting, Outsourced Programming, Python Web Consulting, SEO
IT Consulting, Open Source Software Consulting, Philippines
   © 2003-2015 Neotitans Technologies Inc.