0

I'm trying to get the following global variable storage_i to be accessible to the lvl1 function, I have been able to pass it to other functions inside of the class birdofprey but I can't get it outside of that framework. I have looked around at using global variables between functions, etc, and I have also seen global var usage discouraged. But, basically I am trying to have the value of storage_i summed up from all the threads. So if that could be done without a global variable that would also be great. Any help would be appreciated.

The Code: https://github.com/eWizardII/homobabel/blob/master/Experimental/demo_async_falcon.py

0

2 Answers 2

1

Replace this:

global storage_i
storage_i = i 

With this:

birdofprey.storage_i = i

You also have a typo at line 75:

storage_ii = stroage_i + storage_ii

(stroage_i was intended to be storage_i)

This line should actually be:

storage_ii =  birdofprey.storage_i + storage_ii

EDIT: Also, without having looked closely at it, your use of a class attribute (storage_i) looks like it's susceptible to race conditions. Consider using mutexes to guard accesses to that attribute. I think you also need to wait for the threads to finish executing before you can access the values.

But I'm not sure if a global (or class attribute) is really what you want. I think what you really want is a thread-local variable that you can access after the thread has finished (see the Thread.join method.) If I'm reading that correctly, then forget what I wrote above about mutexes. Instead, set the storage_i attribute as self.storage_i (creating a separate instance for each thread.) Then in the for loop where you're summing the values, access the value as urlv.storage_i. Again, it looks like it's important that you perform a join on each thread before you try to access its values.

That's all the help I can offer for now; perhaps tomorrow morning (my time) I can check in again.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, I set that up however I think urlv.storage_i doesn't work, currently I tried urlv.self, etc, still looking into what else might fix it since it claims that AttributeError: 'birdofprey' object has no attribute 'storage_i' Code: github.com/eWizardII/homobabel/blob/master/Experimental/… (you can ignore that it says urlvself atm I was just testing different things)
The issue seems to be that I can access variables that are under the function run, but I can access those under the class birdofprey.
Okay so I was able to get it to work, I needed to use locking on the threads it seems, though I'll try it later without locking, but for now it works this way. Thanks. Code: github.com/eWizardII/homobabel/blob/master/Experimental/…
Or rather, the locking make it work, but that means that there aren't multiple threads running anymore since the second thread is waiting on the first.
So basically fixed the issue with summing the numbers, now new problem about only two threads are running. stackoverflow.com/questions/4360636/…
0

If you want to access storage_i under lvl1() then use:

birdofprey.storage_i

Well storage_i is not a global variable, its a class attribute.

5 Comments

Okay thanks, but then how do I get access to it when it's set to i under def run(self): ?
don't use global storage_i, you have to access it using self.storage_i
Okay, when I set it to just store as @storage_i = i@ in the function and access it under @lvl1()@ with @storage_ii = birdofprey.self.storage_i + storage_ii@ it claims that - type object 'birdofprey' has no attribute 'self' Code: github.com/eWizardII/homobabel/blob/master/Experimental/…
You removed the definition of storage_i that you'd had in the lvl1() function. (I think it was simply: storage_i = 0) Without that, then indeed there is no storage_i attribute. Also, you don't need self. Just birdofprey.storage_i works.
Okay, I made those corrections, however it just prints 0's. Code: github.com/eWizardII/homobabel/blob/master/Experimental/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.