Python
  Home arrow Python arrow Page 2 - 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 - Using the Threading Module
    ( Page 2 of 4 )

    The threading module provides an easy way to work with threads. Its Thread class may be subclassed to create a thread or threads. The run method should contain the code you wish to be executed when the thread is executed. This sound simple, right? Well, it is:

    import threading

    class MyThread ( threading.Thread ):

       def run ( self ):

          print 'Insert some thread stuff here.'
          print 'It'll be executed...yeah....'
          print 'There's not much to it.'

    Executing the thread is also simple. All we have to do is create an instance of our thread class and then call its start method:

    import threading

    class MyThread ( threading.thread ):

       def run ( self ):

          print 'You called my start method, yeah.'
          print 'Were you expecting something amazing?'

    MyThread().start()

    Of course, it's no fun having just one thread. Just like us humans, threads get lonely after a while. Let's create a group of threads:

    import threading

    theVar = 1

    class MyThread ( threading.Thread ):

       def run ( self ):

          global theVar
          print 'This is thread ' + str ( theVar ) + ' speaking.'
          print 'Hello and good bye.'
          theVar = theVar + 1

    for x in xrange ( 20 ):
       MyThread().start()

    Now let's actually do something semi-useful with the threading module. Servers often use threads to handle multiple clients at once. Let's build a simple but extendable server. When a client opens a connection with the server, the server will create a new thread to handle the client. To send the client's data to the thread, we will need to override the Thread class's __init__ method to accept parameters. The server will now send the thread on its way and then wait for new clients. Each thread will send a pickled object to the appropriate client and then print no more than ten strings received from the client. (A pickled object is basically an object that has been reduced to a few characters. This is useful for storing objects for later use and for sending objects over a network).

    import pickle
    import socket
    import threading

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

    # Our thread class:
    class ClientThread ( threading.Thread ):

       # Override Thread's __init__ method to accept the parameters needed:
       def __init__ ( self, channel, details ):

          self.channel = channel
          self.details = details
          threading.Thread.__init__ ( self )

       def run ( self ):

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

    # 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:
       channel, details = server.accept()
       ClientThread ( channel, details ).start()

    Now we need to build a client that connects to the server, retrieves the pickled object, reconstructs the pickled object and finally sends ten messages, closing the connection:

    import pickle
    import socket

    # Connect to the server:
    client = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
    client.connect ( ( 'localhost', 2727 ) )

    # Retrieve and unpickle the list object:
    print pickle.loads ( client.recv ( 1024 ) )

    # Send some messages:
    for x in xrange ( 10 ):
       client.send ( 'Hey. ' + str ( x ) + '\n' )

    # Close the connection
    client.close()

    Of course, the above client doesn't take advantage of our server's multi-threading capabilities. Only one thread is spawned, which really defeats the purpose of multi-threading. Let's thread the client to make things a bit more interesting. Each thread will connect to the server and execute the code above:

    import pickle
    import socket
    import threading

    # Here's our thread:
    class ConnectionThread ( threading.Thread ):

       def run ( self ):

          # Connect to the server:
          client = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
          client.connect ( ( 'localhost', 2727 ) )

          # Retrieve and unpickle the list object:
          print pickle.loads ( client.recv ( 1024 ) )

          # Send some messages:
          for x in xrange ( 10 ):
             client.send ( 'Hey. ' + str ( x ) + '\n' )

          # Close the connection
          client.close()

    # Let's spawn a few threads:
    for x in xrange ( 5 ):
       ConnectionThread().start()



     
     
    >>> 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 4 Hosted by Hostway
    Stay green...Green IT