Home
| Calendar
| Mail Lists
| List Archives
| Desktop SIG
| Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings Linux Cafe | Meeting Notes | Linux Links | Bling | About BLU |
Hi all you pynuts... I'm playing around with the threading module and wrote a simple test to see python threads in action. The supprize was that it was easy to implement, but I didn't get the full mult-thread benefit that I thought I would get. Basically, I have a counter object which counts up private instance of an integer. I then spawn N number of these threads, expecting the real time execution to be constant up to the number of CPU's on my system. What I found out was that spawning 1 thread, I use up 99% of a CPU, if I spawn 2 threads, I get 115%, (i.e. 1 whole CPU and 15% of another.) Spawning 3 threads, I get 115%.... So I'm wondering why the python thread does not use up two full CPU's? My code, and some time commands follow... #!/usr/bin/env python # Copyright (c) GPL. # This code prototypes the use of threads in python import threading import optparse import sys class Counter(threading.Thread): def __init__(self): threading.Thread.__init__(self) self._counter=0 def run(self): while 1: self._counter+=1 #if (self._counter%1000000 == 0): # print self._counter if (self._counter==10000000): return if __name__ == '__main__': print 'pythread test' print sys.argv nbOfThreads=int(sys.argv[1]) ThreadList=[]; for i in range(nbOfThreads): ThreadList.append(Counter()) ThreadList[i].start() for i in range(nbOfThreads): ThreadList[i].join() [adler at telacode pythreads]$ time ./pythread.py 1 pythread test ['./pythread.py', '1'] real 0m5.584s user 0m5.398s sys 0m0.159s [adler at telacode pythreads]$ time ./pythread.py 2 pythread test ['./pythread.py', '2'] real 0m13.481s user 0m12.290s sys 0m2.951s [adler at telacode pythreads]$ time ./pythread.py 3 pythread test ['./pythread.py', '3'] real 0m19.648s user 0m17.757s sys 0m4.899s -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.
BLU is a member of BostonUserGroups | |
We also thank MIT for the use of their facilities. |