threads

Bart Nessux bart_nessux at hotmail.com
Tue Jan 27 19:47:39 EST 2004


Thank you both for the info on threads in Python it was very helpful to 
me, I've written something that I think can benefit from threading... 
here is the code, I copied this from a few books and a few Web examples 
and modified it to suit my test:

class trivialthread(threading.Thread):
    def url_open(self):
       for x in xrange(999999):
          f = urllib.urlopen("http://xxx.xxx.xxx")
          f.read()
          f.close()
          print self, x

if __name__ == '__main__':
    threads = []
    for x in range(15):
       thread = trivialthread()
       threads.append(thread)
    for thread in threads:
        thread.start()
    while threading.activeCount() > 0:
        print str(threading.activeCount()), "threads running incl. main"
        time.sleep(1)

However, when this runs, the output looks like this:

16 threads running incl. main
1 threads running incl. main
1 threads running incl. main
1 threads running incl. main
1 threads running incl. main
1 threads running incl. main
1 threads running incl. main
...
...

Why does the 16 appear initially and then be replaced by 1? Are there no 
longer 16 threads running? If so, why not?

TIA!!!




More information about the Python-list mailing list