By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,247 Members | 1,864 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,247 IT Pros & Developers. It's quick & easy.

Need help in updating a global variable by a thread

P: n/a
Hello Folks,

My first posting here and I am a stuck in figuring out the exact way
to update a global variable from within a function that doesnt return
any value (because the function is a target of the thread and I dont
know how exactly return would work in such a case). I am sure I am
missing something very fundamental here. The essential pieces of my
code that cause the problem would be something like this:
---------------------------------------------
lookuptab = {'1.9.7.3':'Bangkok','1.9.60.3':'Sydney'}

results = {}

for val in lookuptab.values():
results[val]=0

def testt(loc):
global results
results[loc] = 1
return results[loc]

for x in lookuptab.values():
thread = threading.Thread(target=testt,args=(x))
thread.start()
print results
-------------------------------------------------------

results contain 0 instead of 1. Any help clearing my block is greatly
appreciated.

-Stephen

Oct 17 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
de***********@gmail.com a écrit :
Hello Folks,

My first posting here and I am a stuck in figuring out the exact way
to update a global variable from within a function that doesnt return
any value (because the function is a target of the thread and I dont
know how exactly return would work in such a case). I am sure I am
missing something very fundamental here. The essential pieces of my
code that cause the problem would be something like this:
---------------------------------------------
lookuptab = {'1.9.7.3':'Bangkok','1.9.60.3':'Sydney'}

results = {}

for val in lookuptab.values():
results[val]=0

def testt(loc):
global results
results[loc] = 1
return results[loc]

for x in lookuptab.values():
thread = threading.Thread(target=testt,args=(x))
thread.start()
print results
-------------------------------------------------------
"Would be" ?

I had to fix a couple problems to get your code running (namely,
importing threading and passing correct args to threading.Thread). Do
yourself a favour: next time, take time to post *working* code.

Anyway... Here's a (corrected) version with a couple prints here and
there. I think the output is clear enough:

import threading
import time

lookuptab = {'1.9.7.3':'Bangkok','1.9.60.3':'Sydney'}
results = dict((val, 0) for val in lookuptab.values())

def testt(loc):
global results
print "t-%s before: %s" % (loc,results)
results[loc] = 1
print "t-%s after: %s" % (loc,results)

def main():
for x in lookuptab.values():
thread = threading.Thread(target=testt,args=(x,))
thread.start()

print "main - no sleep: %s" % results
time.sleep(1)
print "main - 1s later : %s" % results

if __name__ == '__main__': main()

And the output is:

main - no sleep: {'Bangkok': 0, 'Sydney': 0}
t-Bangkok before: {'Bangkok': 0, 'Sydney': 0}
t-Bangkok after: {'Bangkok': 1, 'Sydney': 0}
t-Sydney before: {'Bangkok': 1, 'Sydney': 0}
t-Sydney after: {'Bangkok': 1, 'Sydney': 1}
main - 1s later : {'Bangkok': 1, 'Sydney': 1}
Now if I may give you an advice about threads and globals (or any other
kind of shared state): learn about semaphores. While this may not be an
issue in this snippet, race conditions is definitively something you
want to avoid whenever possible and cleanly handle else.

HTH
Oct 17 '07 #2

P: n/a
On Oct 17, 7:48 pm, dedalusena...@gmail.com wrote:
Hello Folks,

My first posting here and I am a stuck in figuring out the exact way
to update a global variable from within a function that doesnt return
any value (because the function is a target of the thread and I dont
know how exactly return would work in such a case). I am sure I am
missing something very fundamental here. The essential pieces of my
code that cause the problem would be something like this:
---------------------------------------------
lookuptab = {'1.9.7.3':'Bangkok','1.9.60.3':'Sydney'}

results = {}

for val in lookuptab.values():
results[val]=0

def testt(loc):
global results
results[loc] = 1
return results[loc]

for x in lookuptab.values():
thread = threading.Thread(target=testt,args=(x))
thread.start()
print results
When I try to run this I get a 6-arguments-instead-of-1 error because
you wrote args=(x) instead of args=(x,). I wonder why you don't see
this error?

Apart from that the code works, except you need to wait for the
threads to finish executing before you see the updated results. Have a
look at threading.join to see how you might do that.

--
Paul Hankin

Oct 17 '07 #3

P: n/a
First off, apologies for posting code that had issues. My bad and
promise next time to do due diligence.
>learn about semaphores.
Definitely will.
>While this may not be an issue in this snippet
Even when more than one user concurrently launches this python
program? Let me read up on this like you suggested and then get back.

Thanks for all your help,
-Stephen
On Oct 17, 4:57 pm, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
dedalusena...@gmail.com a écrit :


Hello Folks,
My first posting here and I am a stuck in figuring out the exact way
to update a global variable from within a function that doesnt return
any value (because the function is a target of the thread and I dont
know how exactly return would work in such a case). I am sure I am
missing something very fundamental here. The essential pieces of my
code that cause the problem would be something like this:
---------------------------------------------
lookuptab = {'1.9.7.3':'Bangkok','1.9.60.3':'Sydney'}
results = {}
for val in lookuptab.values():
results[val]=0
def testt(loc):
global results
results[loc] = 1
return results[loc]
for x in lookuptab.values():
thread = threading.Thread(target=testt,args=(x))
thread.start()
print results
-------------------------------------------------------

"Would be" ?

I had to fix a couple problems to get your code running (namely,
importing threading and passing correct args to threading.Thread). Do
yourself a favour: next time, take time to post *working* code.

Anyway... Here's a (corrected) version with a couple prints here and
there. I think the output is clear enough:

import threading
import time

lookuptab = {'1.9.7.3':'Bangkok','1.9.60.3':'Sydney'}
results = dict((val, 0) for val in lookuptab.values())

def testt(loc):
global results
print "t-%s before: %s" % (loc,results)
results[loc] = 1
print "t-%s after: %s" % (loc,results)

def main():
for x in lookuptab.values():
thread = threading.Thread(target=testt,args=(x,))
thread.start()

print "main - no sleep: %s" % results
time.sleep(1)
print "main - 1s later : %s" % results

if __name__ == '__main__': main()

And the output is:

main - no sleep: {'Bangkok': 0, 'Sydney': 0}
t-Bangkok before: {'Bangkok': 0, 'Sydney': 0}
t-Bangkok after: {'Bangkok': 1, 'Sydney': 0}
t-Sydney before: {'Bangkok': 1, 'Sydney': 0}
t-Sydney after: {'Bangkok': 1, 'Sydney': 1}
main - 1s later : {'Bangkok': 1, 'Sydney': 1}

Now if I may give you an advice about threads and globals (or any other
kind of shared state): learn about semaphores. While this may not be an
issue in this snippet, race conditions is definitively something you
want to avoid whenever possible and cleanly handle else.

HTH- Hide quoted text -

- Show quoted text -

Oct 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.