Threading question

David Lees debl at nospmmytheworld.com
Fri May 4 06:40:48 CEST 2001


I am struggling to improve the performance of some python code that has
2 threads and runs on a dual processor linux (redhat 7.0) box.  I am
using Python 2.1 and can not seem to get the cpu utilization up above
40% for each processor.  Here is a simple example that shows the essence
of the problem.  This is my first try at dual processor code and the
performance indicates I am doing something wrong.  The thing that really
seems strange and makes me wonder if the problem is python's
implementation of threads or something wierd in RedHat 7.0 linux, is
that when I run as a single tread I see 'top' flipping back and forth
between the 2 cpus, utilizing each at 99% and the other at 0.  Any
advice is welcome.

Thanks in advance.

david lees

-------------
def load(n):
    sum=0.0
    for i in range(n):
        sum = sum+math.sqrt(i)**3.5 #junk that loads the cpu

if __name__=='__main__':
    num=string.atoi(sys.argv[1])
    thread_1=threading.Thread(target=load,args=(num,))
    thread_1.start()

    thread_2=threading.Thread(target=load,args=(num,))
    thread_2.start()

    print 'Threads started'



More information about the Python-list mailing list