Subject: Re: Looking for threading tutorial/HOWTO - DN [1]
aahz@netcom.com (Aahz Maruch) - 08 Jan 2000 - comp.lang.python
In article <m3zougva3q.fsf@havenrock.com>, Matt Gushee <mgushee@havenrock.com> wrote: > >I would like to learn how to program threads in Python. Or maybe I >should say, I'd like to use threads in a Python program, so I need to >learn how they work. Can anyone recommend a good online >introduction to the subject (I like books but can't afford to buy one >just now)? Code examples might be good, too, if they are well commented. This following code example is not well-documented, but should be pretty easy to follow; it's designed to test a web server with multiple clients: import urllib, time import threading retrieveDoc = 1 numPings = 20 numThreads = 5 url = 'http://www.foo.com/' class ping ( threading.Thread ) : def __init__ ( self, url, numPings, retrieveDoc=1 ) : self.url = url self.numPings = numPings self.retrieveDoc = retrieveDoc threading.Thread.__init__(self) def run ( self ) : StartTime = time.time() for i in range(self.numPings): page = urllib.urlopen ( self.url ) if self.retrieveDoc: page.read() page.close() EndTime = time.time() self.TotalTime = EndTime - StartTime if __name__ == '__main__' : threadList = [] for i in range(numThreads) : thread = pingPing ( url, numPings, retrieveDoc ) threadList.append ( thread ) StartTime = time.time() for thread in threadList : thread.start() while threading.activeCount() > 1 : print ".", time.sleep(1) EndTime = time.time() TotalTime = EndTime - StartTime print TotalPings = 0 ThreadTime = 0 for thread in threadList : TotalPings = TotalPings + thread.numPings ThreadTime = ThreadTime + thread.TotalTime PingAvg = TotalPings / TotalTime ResponseAvg = ThreadTime / TotalPings print "Threads: ", numThreads print "Pings:", TotalPings print "Total time:", TotalTime print "Pings per second:", PingAvg print "Average response time (secs):", ResponseAvg -- --- Aahz (@netcom.com) Androgynous poly kinky vanilla queer het <*> http://www.rahul.net/aahz/ Hugs and backrubs -- I break Rule 6 The problem with an ever-changing .sig is that you have to keep changing it.
Last modified
2000-02-10
2000-02-10
(195.108.246.50)
Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated
