About socket threading

Steve Holden steve at holdenweb.com
Tue Jan 17 08:01:09 EST 2006

Kr z wrote:
> Hi,
> Do anyone know the Python source codes on how the client can send/"pump"
> a lot of threads to the server class?
It isn't that difficult. Here's a basic threading framework that allows 
you to vary the number of threads fairly easily. Then you just have to 
make sure that each thread doesn't interfere with the others. Hope this 

The run() method is the network client tasks you want to run.

I have used this scheme with a 200-SMTP-client setup and it worked very 
well, with the worker threads communication back to the master thread 
using Queue.Queue.

import time, threading, random

class MyThread(threading.Thread):
     """ Each thread picks a 'random' integer between 0 and 19 and reports
         in once per second for that many seconds.

     def run(self):
         iterations = random.randint(0, 19)
         print "Thread", self.getName(), "starting", iterations, 
         for i in range(iterations):
             print "   ", self.getName(), "is reporting "
         print self.getName(), "is DONE"

def test():
     threadList = []

     # Create 5 MyThread() threads
     for i in range(5) :
         thread = MyThread()

     # Start all threads
     for thread in threadList:

     # As long as we have more than just the 'main' thread running, 
print out
     # a status message
     while threading.activeCount() > 1 :
         print str(threading.activeCount()), "threads running including 

if __name__ == '__main__':

Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/

More information about the Python-list mailing list