[英]How to get multiprocessing Array in python process
I'd like to access the "gs" variable here in each process. 我想在每个进程中访问“ gs”变量。 How do I reference the initargs from the process? 如何从过程中引用initargs?
# file exec.py
import time
import os
import random
def f(letter):
sleep_time = random.randint(3, 20)
pid = os.getppid()
g_id = # gs.pop() (how to access gs?)
print('-------\nsleep: {}\npid: {}\nletter: {}'.format(sleep_time, pid, letter))
time.sleep(sleep_time)
print('===========\n{} ended\n==========='.format(letter))
# gs.add(g_id) (return g_id so it can be used by others)
return letter
This is the main file: 这是主文件:
from multiprocessing import Pool, TimeoutError, Array
# file main.py
if __name__ == '__main__':
gs = Array('B', [66, 67, 68, 69], lock=False)
pool = Pool(processes=4, initargs=(gs, ))
hopts = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
pool.map(f, hopts)
The key thing you are missing is that you need to use the initializer argument of the pool
. 您缺少的关键是需要使用pool
的初始化参数。 This answer is a good example of how to accomplish that and why you need to do it that way. 这个答案是一个很好的例子,说明了如何做到这一点以及为什么需要那样做。
Here is an example of applying that kind of a workflow to your problem: 这是将此类工作流程应用于您的问题的示例:
from multiprocessing import Pool, Array
import time
import os
import random
def f(letter):
sleep_time = random.randint(3, 20)
pid = os.getppid()
g_id = gs[0]
print('-------\nsleep: {}\npid: {}\nletter: {}'.format(sleep_time, pid, letter))
time.sleep(sleep_time)
print('===========\n{} ended\n==========='.format(letter))
# gs.add(g_id) (return g_id so it can be used by others)
return letter
def init(local_gs):
global gs
gs = local_gs
if __name__ == '__main__':
gs = Array('B', [66, 67, 68, 69], lock=False)
pool = Pool(initializer=init, initargs=(gs,))
hopts = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
pool.map(f, hopts)
(I'm not sure what your end goal is exactly so I attempted to modify as little as possible.) (我不确定您的最终目标是什么,因此我尝试尽可能少地进行修改。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.