from threading import Thread
import time
def print_k():
while true:
if main.k % 2 == 1: # ditto
print(main.k, "is even.") # <-- my problem is HERE ( Ignore all the other stuff )
time.sleep(2)
def main():
k = 1
while k != 200:
k += 1
print k
time.sleep(0.5)
if __name__ == '__main__':
Thread(target=print_k).start()
Thread(target=main).start()
in this script (example only, ignore all realistic functionality) I am trying to run main()
, which adds up to 200 and prints it, and in print_k
, i am printing main
's variable, k.
I have an exception raised, unsurprisingly, and am wondering how i can access a separate function's variable from a different function (they are both running at the same time, by the way, hence the Threading module.)
You can't print
main
's variablek
. The whole point of local variables is that they're local. It doesn't matter whether they're running at the same time or not; they each have their own separate local environment. (In fact, if you callmain
60 times, each of those 60 calls has its own local environment.)But there are a number of things you can do.
The simplest, but generally worst, is to use global variables instead of local variables. Just add
global k
to the top of themain
function, add some start value fork
at the top level (before either thread starts), and you can now access the same global variable insideprint_k
.Bundling the shared state and functions up together in a class, where both functions become methods that can access
self.k
, is a better solution. Passing in some kind of mutable "holder" to bothmain
andprint_k
is also a better solution. Redesigning your app around explicit message passing (e.g., on aQueue.Queue
) is even better.I'll show how to do it with a class:
Now, because we're using
self.k
, an attribute of theKCounter
instance, instead ofk
, a local variable, both methods see the same variable.