I have main.py which looks like the following:
...
val1, val2 = None, None
...
thread1 = threading.Thread(func1, )
thread1.start()
while condition:
continue
else:
thread1.join()
#check the values of val1 and val2 after the thread had changed them
if val1!= None and val2!= None:
do_something()
on the other hand func1 is defined in main.py script as follows:
...
def func1():
global val1, val2
val1, val2 = process_something_and_return_values()
My question will defining func1() as I do will guarantee to find val1 and val2 changed and not assigned to None?
if not how to go about this kind of setting to change values of variables through a thread
PS: using Python 2.7
UPDATE: Python doesn’t have the ability to change variables across modules. Therefore I want to check if the code is correct if it is in the same module?
>Solution :
Explicitly define val1 and val2 as globals in function_helpers.py:
val1, val2 = None
... # etc.
Then in main.py import just the module function_helpers and then refer to variables and functions within that module. In this way both modules are sharing the same val1 and val2 instances:
import function_helpers
...
function_helpers.val1, function_helpers.val2 = None, None
...
thread1 = threading.Thread(target=function_helpers.func1)
thread1.start()
while condition:
continue
else:
thread1.join()
#check the values of val1 and val2 after the thread had changed them
if function_helpers.val1!= None and function_helpers.val2!= None:
do_something()