[英]pool.apply_async and global variable
Because i cant run simply program with pool.apply_async and global variable, I need a help.因为我不能简单地使用 pool.apply_async 和全局变量运行程序,所以我需要帮助。 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.这个程序应该如何工作:变量 config.variable 是一个标志 - 默认为 False。 If something goes wrong in thread value of this flag should be set to Thru and value True should stop/Pause.如果此标志的线程值出现问题,则应将其设置为 Thru,值 True 应停止/暂停。 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.我阅读了类似的线程,例如这个Python 共享值或带有共享变量(值)的 Python 多处理 Pool.apply_async但有关于将参数传递到线程中。 Most of others examples use Value or Manager but with Process and this solution work for me.大多数其他示例使用 Value 或 Manager 但使用 Process 并且此解决方案对我有用。
Question are: Can I use Value or Manager for pool.apply_async?问题是:我可以将 Value 或 Manager 用于 pool.apply_async 吗? How to do it for changing a global variable.如何更改全局变量。 What kinde of typecodes should I use for boolean True and False我应该为布尔值 True 和 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.我无法更改 apply_async 以进行处理。
File config.py文件 config.py
variable1 = False
File main.py文件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?在这种情况下如何使用 Value 或 Manager? Can somebody add few line?有人可以添加几行吗?
Tank you for help and explanation.坦克你寻求帮助和解释。
Solution for someone who will need it.为需要它的人提供解决方案。 I edited example from 16.6.1.4.我从16.6.1.4编辑了示例。 Sharing state between processes¶ 进程间共享状态¶
from multiprocessing import Pool, Manager
import config
from threading import Thread
import time
def f(d):
if d['flag1'] is False:
d['flag1'] = True
else:
d['flag1'] = False
# l.reverse()
def stop():
print('stop')
while 1:
if config.variable1 is True:
break
if __name__ == '__main__':
manager = Manager()
print(config.variable1)
d = manager.dict()
thread1 = Thread(target = stop)
thread1.start()
while 1:
d['flag1'] = config.variable1
pool = Pool(4)
p = pool.apply_async(f, args=(d,))
pool.close()
pool.join()
config.variable1 = d['flag1']
print (d)
print(config.variable1)
if thread1.is_alive() is False:
break
time.sleep(3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.