Microthreads

Gary Richardson garyr at fidalgo.net
Sat May 17 16:54:39 EDT 2003


In David Mertz's article on microthreads
http://www-106.ibm.com/developerworks/linux/library/l-pythrd.html
the script in Listing 2 (appended below) attempts to characterize the
overhead of his scheme. It creates 100,000 threads and measures the time
to perform one million context switches. On my computer (400 MHz AMD-K6,
Win98 SE, 192 MB ram) this took about 40 seconds. The article states that
on a 366 MHz Pentium II laptop the time was about 10 seconds. I'm using
Python 2.2, Pythonwin, win32all build 146.
Is my computer really sick?


from __future__ import generators
import sys, time
threads = []
TOTALSWITCHES = 10**6
NUMTHREADS = 10**5

def null_factory():
    def empty():
        while 1: yield None
    return empty()

def quitter():
    for n in xrange(TOTALSWITCHES / NUMTHREADS):
        yield None

def scheduler():
    global threads
    try:
        while 1:
            for thread in threads: thread.next()
    except StopIteration:   pass

if __name__ == "__main__":
    for i in range(NUMTHREADS):
        threads.append(null_factory())
    threads.append(quitter())
    starttime = time.clock()
    scheduler()
    print 'total time:', time.clock() - starttime















More information about the Python-list mailing list