[CentralOH] Python threads

Bryan harrisbw at notes.udayton.edu
Mon Aug 4 20:46:03 CEST 2008


Thanks!  I never would have figured that one out myself.  It makes sense
now.  

I think the threads _were_ actually running concurrently, they just
weren't being given a number based on their start order.

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