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

 
Developer Updates  
Free Website Content 
 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: starstarstarstarstar / 125
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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
     

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT