If you want your application to perform several tasks at once, you can use threads. Python can handle threads, but many developers find thread programming to be very tricky. Among other points, Peyton McCullough covers how to spawn and kill threads in this popular language.
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 ):
# 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()