[Tutor] Another question about Variables in Threaded functions

lonetwin lonetwin@yahoo.com
Mon, 15 Oct 2001 15:57:25 +0530


Hi all,
    I asked a question a couple o' days back about class variables when one 
uses threads...an' I learned (thanx to Boudewijn and Kirby) about locking 
...now I want to know this ...suppose I call a function from the run() method 
of my threaded class...and pass to it a local (local to run()) variable,
a) how should I be doing this ?? (what should I keep in mind ??)
b) what happens to these local variable (and to the local variables of the 
function being called) ??..are these too shared between threads ??

OK...Ok.... I know I'm not too good at framing questions....so like I did 
earlier ..here's some pseudo-code:
==============================================
import threading
import time

class ThreadedFoo(threading.Thread):
    FooBox = range(100)
    def __init__(self, lock):
        threading.Thread.__init__(self)
        self.lock = lock

    def run(self):
        print "Started ", self.getName()
        while 1:
	
            self.lock.acquire()
            try:
                if len(self.FooBox) == 0:
                    break
                Goody = self.FooBox.pop()  # Is Goody shared ?? I think not
                                           # Am I wrong ??
                self.doStuff(Goody)        # what happens here ??
            finally:
                self.lock.release()
            time.sleep(1)

    def doStuff(self, thingy):
        BoxOStuff = ['pink', 'yellow', 'green', 'blue', 'red' ]
        # Is ^^^^ this shared ?? ...again I don't think so..but I'm not sure
        for item in BoxOStuff:       # What is happening here ??
            print self.getName(), thingy, item

if __name__ == '__main__':
    FooThreadBag = []
    SharedLock = threading.Lock()
    for x in range(3):
        thread = ThreadedFoo(SharedLock)
        FooThreadBag.append(thread)

    for thread in FooThreadBag:
        thread.start()

    print "done"

============================================
    So, that's it...Kirby's explanation leads me to believe that 'Goody'
and 'BoxOStuff' are not shared....but I want someone to actually tell me 
that...anyways, thanx for your time.

Peace
Steve

----------------------------------------------
Every oak tree started out as a 
couple of nuts who stood their ground.
                                    Anonymous
----------------------------------------------