[CentralOH] Python threads
Bryan
harrisbw at notes.udayton.edu
Mon Aug 4 20:51:41 CEST 2008
Yep Thanks! It wasn't somehow blocking somewhere as I had
suspected. :-p It was doing what I told it to do....
This works as well without the init:
import threading
import time
import random
theVar = 1
class MyThread ( threading.Thread ):
def run ( self ):
global theVar
myNumber = theVar
theVar = theVar + 1
time.sleep(random.random()*10)
print 'This is thread ' + str ( myNumber ) + ' speaking.\nHello
and good bye.'
for x in xrange ( 200 ):
MyThread().start()
On Mon, 2008-08-04 at 14:23 -0400, Steven Huwig wrote:
> Your global counter is the problem. You should give the thread its own
> number before you start the thread.
>
> import threading
> import time
> import random
>
> class MyThread ( threading.Thread ):
> def __init__(self, num):
> threading.Thread.__init__(self)
> self.num = num
>
> def run ( self ):
> time.sleep(random.random()*10)
> print 'This is thread ' + str ( self.num ) + ' speaking.'
> print 'Hello and good bye.'
>
> for x in xrange ( 20 ):
> MyThread(x).start()
--
Bryan Harris
Research Engineer
Structures and Materials Evaluation Group
harrisbw at notes.udayton.edu
http://www.udri.udayton.edu/
(937) 229-5561
More information about the CentralOH
mailing list