Need help with threads

Damian Menscher menscher+python at uiuc.edu
Wed Jun 13 14:35:38 EDT 2001


I'm trying to use threads to run various processes that require some
I/O.  This is apparently broken.  Here's a simple program:

=================================================

#!/usr/bin/python
import time
import thread
import popen2

def b(n):
        print "running b(%s)"%n
        r,w = popen2.popen2('cat')
        w.write("test %s"%n)
        w.close()
        print "process %s read %s"%(n,r.readline())

for n in range(10):
        thread.start_new_thread(b, (n,))	### with threads
        #b(n)					### without threads

time.sleep(1)  ## Allow some time for threads to finish

=================================================

So this should start up, fork off 10 threads that just do basic stuff,
then wait a second for everything to finish.

Running *without* threads (by uncommenting the b(n) line) shows
typical (correct) execution:

[menscher at qcdhome t2]$ ./threads.py 
running b(0)
process 0 read test 0
running b(1)
process 1 read test 1
running b(2)
process 2 read test 2
running b(3)
process 3 read test 3
running b(4)
process 4 read test 4
running b(5)
process 5 read test 5
running b(6)
process 6 read test 6
running b(7)
process 7 read test 7
running b(8)
process 8 read test 8
running b(9)
process 9 read test 9

But running *with* threads gives me:

[menscher at qcdhome t2]$ ./threads.py 
running b(6)
running b(2)
running b(4)
running b(0)
running b(5)
running b(1)
Killed

Where the job hogged 99% of CPU and had to be killed (even a ^C
didn't stop it).

I'm guessing there might be a namespace conflict in the I/O, but I'm
new to python in general and can't develop a workaround.

While I'm posting... is there an elegant way to wait till all threads
have finished?  I can't count on things finishing in a fixed time for
my application.

Ideas?  Please??

Damian Menscher
-- 
--==## Grad. student & Sys. Admin. @ U. Illinois at Urbana-Champaign ##==--
--==## <menscher at uiuc.edu> www.uiuc.edu/~menscher/ Ofc:(217)333-0038 ##==--
--==## Physics Dept, 1110 W Green, Urbana IL 61801 Fax:(217)333-9819 ##==--



More information about the Python-list mailing list