[Tutor] simple threads program

calvin exnihilo@myrealbox.com
Fri Jul 18 03:07:02 2003


hi,

I am trying to create a simple threaded program, to learn how threads work.

I would like to download and save a bunch of pages, and since the 
download is most of the time, it seems like a good candidate for speedup 
using threads.

What I have so far is a simple program that I've adapted from thread 
examples that I've seen on the web:

#!/usr/bin/python

import thread, urllib

pages = ['cnn.com', 'nytimes.com', 'slashdot.org', 'kuro5hin.org', 
'news.google.com', 'berkeley.edu']
pages = pages * 3

def getPage(id, p):
    stdoutmutex.acquire()
    sock = urllib.urlopen('http://'+p)
    print "Downloaded", p
    htmlSource = sock.read()
    f = open(p+'.html', 'w')
    f.write(htmlSource)
    f.close()
    stdoutmutex.release()
    exitmutexes[id].acquire() # signal main thread
   
stdoutmutex = thread.allocate_lock()
exitmutexes = []


for i in range(len(pages)):
    exitmutexes.append(thread.allocate_lock())
    thread.start_new(getPage, (i, pages[i]))
    print "Started thread", i

for mutex in exitmutexes:
    while not mutex.locked(): pass

print "Main thread exiting"


This seems to be running as if everything is in one thread, since the 
pages always download in the exact order that they appear in the list of 
pages.

Any hints at what I'm doing wrong?

thanks,

calvin