I'm new to Python programming (using v.3.8.8) and have a very basic question on accessing global variables in a multithreading program using processes. For example, I have the following simple code:
from multiprocessing import Process
global_var = 777
def workerThread():
global global_var
my_file = open("out_file",'w')
print(global_var, file=my_file)
my_file.close()
return
if __name__ == '__main__':
global_var = 999
if (True):
# use multiprocessing, but launch only one thread
procs = Process(target=workerThread, args=())
procs.start()
procs.join()
else:
# standard function call, so these two paths should be equivalent
workerThread()
I can execute it with two code-paths. "True" runs it in multithreaded mode (but with only one thread) and "False" calls the function directly, so the two are esentially equivalent.
Therefore, I had assumed that both would give the same behavior and output "999", because in both cases workerThread() is called after the global variable has been set to 999 (I pipe the output to "out_file" because stdout does not print when multithreading).
But for some reason, the multithreaded approach outputs the original value of 777 while the direct function call outputs 999. This doesn't make any sense to me. Why are they different? How do I fix this?
I initially thought of adding a global global_var
right after if __name__ == '__main__':
in order to ensure that it's setting the global variable (and not a local variable by the same name), but that didn't make sense because the "if" statement is not a separate function, but rather part of the "main" function. Indeed, when I tried this, it gave me a syntax error "SyntaxError: name 'global_var' is assigned to before global declaration"
. So that's clearly not the right answer.
So I am not sure what is going on, and how to do this correctly. It seems like a very simple thing that should be easy to do, but I'm completely stuck and have looked around but found no answer. Any suggestions?
Finally, at the end of the day, I don't want to use a global variable declared in this file, but rather a global variable declared in another file, say "my_globals.py", so I can access the same global variable from multiple files as the program runs. So I had first tried accessing it within the workerThread as my_globals.global_var
but that also didn't work, which led me to simplify until I got to this code.
And in that case, the statement global my_globals.global_var
is invalid (syntax error), so I am not sure how to guarantee that the workerThread function would use the global variable from the other file. Or is it automatically considered a global variable because it's an attribute of my_globals? Trying to figure out if there is something like extern
in C...
Thanks in advance for your help with these n00b questions. I'm just not very good at Python.
--Miguel