使用全局声明的元组进行 Python 多处理

Using globally declared tuple for Python multiprocessing

我在全局使用多处理元组时遇到了一些问题 class。

我有如下生成的代码:

from multiprocessing import Pool

if __name__ == '__main__':
    jj = ()
    def f(x):
        global jj
        jj += (x*x,)

    # Section A
    #for ii in range(20):
    #    f(ii)
    #print (jj)

    # Section B
    pool = Pool(processes=4)
    pool.map(f, range(20))  
    pool.join()
    print (jj)

如果我 运行 部分只有 B,我得到的元组 jj 是一个空元组。但是,如果我 运行 只有 A 部分,我会得到一个长度为 20 的元组。

为什么会这样?

好的,正如 Python multiprocessing global variable updates not returned to parent 所解释的,进程之间不共享全局状态。

您可以共享状态,例如,multiprocessing.Queue

from multiprocessing import Pool, Queue

if __name__ == "__main__":
    jj = ()
    q = Queue()

    def f(x):
        global jj
        jj += (x * x,)

    def f_multi(x):
        q.put(x * x)

    # Section A
    for ii in range(20):
        f(ii)
    print(jj)

    # Section B
    pool = Pool(processes=4)
    pool.map(f_multi, range(20))
    pool.close()

    stop = "STOP"
    q.put(stop)
    items = []
    for i in iter(q.get, stop):
        items.append(i)

    print(tuple(items))

或者,您可以使用 print(tuple(sorted(items))) 以与 Section A 生成的顺序相同的顺序获取值。 4 个进程正在处理 Section B 中的任务,因此 "unordered" 结果。