[CentralOH] Python threads

Neil Ludban nludban at osc.edu
Mon Aug 4 20:08:13 CEST 2008


On Mon, 4 Aug 2008 13:34:58 -0400
"Bryan Harris" <harrisbw at notes.udayton.edu> wrote:
> It is obviously supposed to spawn 20 threads that then sleep for a
> random time.  The proof would be that they report back out of order.
> What really happens is that the threads report back in order with
> random waits in between.  Obviously sleep isn't releasing the current
> thread.

Threading is probably working fine, the example is obviously broken.
Improved, but not perfect:

>    def run ( self ):
> 
>       global theVar
	myVar = theVar
	theVar = theVar + 1
>       time.sleep(random.random()*10)
>       ###print 'This is thread ' + str ( theVar ) + ' speaking.'
        print 'This is thread ' + str ( myVar ) + ' speaking.'
>       print 'Hello and good bye.'
>       ###theVar = theVar + 1



More information about the CentralOH mailing list