Accessing thread variables

Aahz aahz at pythoncraft.com
Sat Feb 22 19:18:45 EST 2003


In article <mailman.1045653475.3600.python-list at python.org>,
Stuart Bishop  <zen at shangri-la.dropbear.id.au> wrote:
>On Wednesday, February 19, 2003, at 09:21  PM, Lukas Kubin wrote:
>>
>> What's the best way to access a thread's variable?
>> Let's say I have thread:
>>
>> class MyThread(threading.Thread):
>>   def __init__(self):
>>     threading.Thread.__init__(self)
>>     self.varToReturn = ""
>>     self.start()
>>
>>   def run(self):
>>     self.varToReturn = myAction()
>>
>>   def myAction():
>>     string = "hello"
>>     return string
>>
>> And I call the thread:
>>
>> i = MyThread()
>> print i.varToReturn
>>
>> This returns nothing (the empty string) instead of "hello".
>> What am I doing bad?
>
>i = MyThread()
>i.join() # Wait for the thread to finish
>print i.varToReturn

Close, but no cigar:

i = MyThread()
i.start()
i.join()
print i.varToReturn
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Register for PyCon now!  http://www.python.org/pycon/reg.html




More information about the Python-list mailing list