Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python多处理中使用全局/公共变量_Python_Python 3.x_Multiprocessing_Python Multiprocessing - Fatal编程技术网

如何在python多处理中使用全局/公共变量

如何在python多处理中使用全局/公共变量,python,python-3.x,multiprocessing,python-multiprocessing,Python,Python 3.x,Multiprocessing,Python Multiprocessing,我最近开始在python中使用多处理,我有以下代码来更新来自多个进程的列表项。但它给出的是一张空名单 from multiprocessing import Pool import time global_list = list() def testfun(n): print('started ', n) time.sleep(1) global_list.append(n) print('completed ', n) def call_multipr

我最近开始在python中使用多处理,我有以下代码来更新来自多个进程的列表项。但它给出的是一张空名单

from multiprocessing import Pool
import time

global_list = list()


def testfun(n):
    print('started ', n)
    time.sleep(1)
    global_list.append(n)
    print('completed ', n)


def call_multiprocessing_function():
    mytasks = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
    with Pool() as pool:
        pool.map(testfun, mytasks)


if __name__ == "__main__":
    print('starting the script')

    print(global_list)
    call_multiprocessing_function()
    print(global_list)

    print('completed the script')
我得到以下输出

starting the script
[]
started  a
started  b
started  c
started  d
completed  a
started  e
completed  b
started  f
completed  c
started  g
completed  d
started  h
completed  e
started  i
completed  f
started  j
completed  g
started  k
completed  h
started  l
completed  i
started  m
completed  j
started  n
completed  k
completed  l
completed  m
completed  n
[]
completed the script

结果列表是空的。是否有一种方法可以在所有这些过程中共享一个公共变量来存储数据。我们如何使用多处理实现此功能?

进程不共享内存。因此您需要使用Manager.list

import time    
from multiprocessing import Pool, Manager

m=Manager()
global_list = m.list()


def testfun(n):
    print('started ', n)
    time.sleep(1)
    global_list.append(n)
    print('completed ', n)


def call_multiprocessing_function():
    mytasks = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
    p=Pool()
    p.map(testfun, mytasks)


if __name__ == "__main__":
    print('starting the script')

    print(global_list)
    call_multiprocessing_function()
    print(global_list)

    print('completed the script')
输出:

starting the script
[]
started  a
started  b
started  c
started  d
started  e
started  f
started  g
started  h
completed  e
started  i
completed  f
started  j
completed  d
started  k
completed  a
started  l
completed  g
started  m
completed  b
completed  c
started  n
completed  h
completed  i
completed  j
completed  k
completed  l
completed  n
completed  m
['e', 'f', 'd', 'a', 'g', 'b', 'c', 'h', 'i', 'j', 'k', 'l', 'n', 'm']
completed the script

使用python3,这就是OP使用的。不,它们是语句,正在打印元组。我通过在我的机器上运行您的代码为您修复了输出。这里没有其他差异要报告。这将在python2上起作用。哦,太棒了!但我发现了下面的错误。RuntimeError:试图在当前进程完成引导阶段之前启动新进程。这可能意味着您没有使用fork启动子进程,并且忘记在主模块中使用正确的习惯用法:if name==“main”:freeze_support()。。。如果程序不会被冻结以生成可执行文件,则可以省略“freeze_support()”行。我认为这个错误是windows平台特有的。我正在使用windows 10(来源:)