Python
  Home arrow Python arrow Page 2 - 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 
 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

    PCmover - $15 Off with Coupon Code CJPH7Q

    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


       · 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...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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