Python
  Home arrow Python arrow Page 3 - Basic Threading in Python
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Basic Threading in Python
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 90
    2005-04-04

    Table of Contents:
  • Basic Threading in Python
  • Using the Threading Module
  • Pooling Threads
  • More Thread Tricks

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Basic Threading in Python - Pooling Threads
    (Page 3 of 4 )

    It's important to remember that threads don't start up instantly. Creating too many of them can slow down your application. It takes time to spawn and later kill threads. Threads can also eat up valuable system resources in large applications. This problem is easily solved by creating a set number of threads (a thread pool) and assigning them new tasks, basically recycling them. Connections would be accepted and then pushed to a thread as soon as it finished with the previous client.

    If you still don't understand, compare it to a doctor's office. Let's say that there are five doctors. These are our threads. Patients (clients)  walk into the office, and if the doctors are busy, they are seated in the waiting room.

    Obviously, we'll need something that can transfer client data to our threads without running into problems (it will need to be “thread safe”). Python's Queue module does this for us. Client information is stored in a Queue object, where threads can pull them out when needed.

    Let's recreate our server to take advantage of a pool of threads:

    import pickle
    import Queue
    import socket
    import threading

    # We'll pickle a list of numbers, yet again:
    someList = [ 1, 2, 7, 9, 0 ]
    pickledList = pickle.dumps ( someList )

    # A revised version of our thread class:
    class ClientThread ( threading.Thread ):

       # Note that we do not override Thread's __init__ method.
       # The Queue module makes this not necessary.

       def run ( self ):

          # Have our thread serve "forever":
          while True:

             # Get a client out of the queue
             client = clientPool.get()

             # Check if we actually have an actual client in the client variable:
             if client != None:

                print 'Received connection:', client [ 1 ] [ 0 ]
                client [ 0 ].send ( pickledList )
                for x in xrange ( 10 ):
                   print client [ 0 ].recv ( 1024 )
                client [ 0 ].close()
                print 'Closed connection:', client [ 1 ] [ 0 ]

    # Create our Queue:
    clientPool = Queue.Queue ( 0 )

    # Start two threads:
    for x in xrange ( 2 ):
       ClientThread().start()

    # Set up the server:
    server = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
    server.bind ( ( '', 2727 ) )
    server.listen ( 5 )

    # Have the server serve "forever":
    while True:
       clientPool.put ( server.accept() )

    As you can see, it's a little more complex than our previous server, but it's not amazingly complicated. The client we created in the previous section can be used to test this server, just like the previous server.

    More Python Articles
    More By Peyton McCullough


       · You can "turn an ordinary function into a thread" using the Thread class: just pass...
       · Thanks for pointing that out to me. :))
       ·  Sir: Would you like to give a "mt" example which is more complicated, more...
       · Sorry for the delay. I should really check comments more often....The...
       · Hi,The example with the threadpool works quite well. Except that I do not manage...
       · Thats a great idea but now that I have done that I cannot see what I type in my...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway