[Tutor] Re: Tutor digest, Vol 1 #2622 - 13 msgs

Cliff Wells logiplex@qwest.net
Fri Aug 1 15:25:03 2003


On Thu, 2003-07-31 at 18:35, G Kiran wrote:
> Hi,
> 
>     I am trying to limit no of threads i can spawn for a multi threaded
> socket server .
> 
> presently i do this by the following fragment of code
> 
> 
> maxthreads=40
> for ct in range(0,len(scanlist)-1):
>         th.append(checkport(scanlist[ct])
>         th[ct].start()
>         while  threading.activeCount() > maxthreads :  pass  ##### this is
> how i am limiting the no of threads
> 
> i am using the threading class fo my checkport function
> 
> is there any other way of doing it...rather than the endless loop which i
> feel wastes the processor time

The general method is to create a pool of threads that wait for work to
become available (for instance, from a Queue).  This has the advantage
of not only controlling the number of threads, but also eliminating the
overhead of starting a thread everytime some work becomes available.

For example:

import threading, Queue, time

work = Queue.Queue()

nthreads = 10

def consumer(n):
    while 1:
        job = work.get()
        if job is None:
            return
        print "thread %d" % n, job
        time.sleep(0.01) # arbitrary delay


def producer(iterations):
    for i in xrange(iterations):
        work.put(i) # some bogus work
        
    for i in xrange(nthreads):
        work.put(None) # kill the threads
        

if __name__ == '__main__':
    # start up a pool of 10 threads
    for i in range(nthreads):
        t = threading.Thread(target = consumer, args = (i,))
        t.start()
        
    # at this point the threads are all idle waiting
    # for work to do, let's give it to them
    producer(1000)


Regards,

-- 
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726  (800) 735-0555