我当前的项目需要使用多个流程。我需要在这些进程之间共享一个数组。数组需要能够在任何时候被写入。数组必须具有多个维度。(例如:[["test",2],[872009873,"howdy"]](我已经找了几个小时的答案了,但什么都找不到。请帮忙。提前感谢!
试试看:
from multiprocessing import Pool, Manager
def worker(v, array):
array.append(["test", v])
def main():
foo = [["test", 2], [87209873, "howdy"]]
array = Manager().list(foo)
with Pool(processes=4) as pool:
pool.starmap(worker, [(i, array)
for i in range(4)])
print(array)
if __name__ == "__main__":
main()
[编辑]
如果你愿意,主程序在计算过程中保持运行,将池封装在一个单独的线程中:
from multiprocessing import Pool, Manager
from threading import Thread
def _worker(v, array):
for i in range(10000):
array.append(["test", v])
def processor(array):
with Pool(processes=4) as pool:
pool.starmap(_worker, [(i, array)
for i in range(4)])
def main():
foo = [["test", 2], [87209873, "howdy"]]
array = Manager().list(foo)
t = Thread(target=processor, args=(array,))
t.start()
print("Good day!")
# Wait, while thread ends.
# Without doing it, you'll print array,
# not knowing when the thread ended.
t.join()
print(array)
if __name__ == "__main__":
main()
首先,list
不是array
,如果您想在不同的进程之间共享list
,可以使用multiprocessing
模块中的Manager
,例如:
import multiprocessing as mp
def remove_last_element(mp_list: list):
mp_list.pop()
def append_list(mp_list: list):
mp_list.append([12, 'New Hello'])
if __name__ == "__main__":
mp_list = mp.Manager().list()
mp_list.append(['Hello'])
print("before multiprocessing:", mp_list)
worker1 = mp.Process(target=remove_last_element, args=(mp_list,))
worker2 = mp.Process(target=append_list, args=(mp_list,))
worker1.start()
worker2.start()
worker1.join()
worker2.join()
print("after multiprocessing:", mp_list)
>>> before multiprocessing: [['Hello']]
>>> after multiprocessing: [[12, 'New Hello']]