Because i cant run simply program with pool.apply_async and global variable, I need a help. I cant resolve a problem with shared memory. Short description a program flow:
How should this program works: Variable config.variable is a flag - default False. If something goes wrong in thread value of this flag should be set to Thru and value True should stop/Pause. In other words Idea is that fail inside a async proces should stop/pause program.
I tired do something with multiprocessing Value and Manager but with no result. I do not know it is my fail or it will never work. I am too weak for solving this problem. Not enough skills. Selv-learning is hard. I readed similar thread for example this Python share values or Python multiprocessing Pool.apply_async with shared variables (Value) but there is about passing argument into a thread. Most of others examples use Value or Manager but with Process and this solution work for me.
Question are: Can I use Value or Manager for pool.apply_async? How to do it for changing a global variable. What kinde of typecodes should I use for boolean True and False
I read this : Sharing state between processes
Please help me and show how should i write it. I attache simply code. Can somebody edit it and add missing lines? I can not change apply_async to process.
File config.py
variable1 = False
File main.py
import config
from multiprocessing import Pool
import time
import sys
def func2():
try:
config.variable1 = True
print('Global Variable in thread {}'.format(config.variable1))
except Exception as e:
print(e)
if __name__ == '__main__':
while 1:
time.sleep(1)
try:
pool = Pool(4)
pool.apply_async(func2)
pool.close()
pool.join()
except Exception as e:
print(e)
# print(config.variable1)
print('Global Variable in main loop {}'.format(config.variable1))
if config.variable1 is True:
sys.exit(0)
How to use Value or Manager in this case? Can somebody add few line?
Tank you for help and explanation.