【问题标题】:How to have a global/common variable in python multiprocessing如何在 python 多处理中拥有一个全局/公共变量
【发布时间】:2018-03-15 05:35:02
【问题描述】:

我最近开始在 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

结果列表为空。有没有办法让所有这些进程共享一个公共变量来存储数据。我们如何使用多处理来实现此功能?

【问题讨论】:

    标签: python python-3.x multiprocessing python-multiprocessing


    【解决方案1】:

    进程不共享内存,所以需要使用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(来源:stackoverflow.com/questions/18204782/…
    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 2016-11-14
    • 2012-06-28
    • 1970-01-01
    相关资源
    最近更新 更多