python - 在多处理模块中使用 Pool 修改全局变量

我不熟悉多处理模块。我正在尝试验证不同过程中的变量是否无关紧要。经过测试,我发现不同的进程很可能“共享”变量。当进程具有相同的 pid 时会发生这种情况。不知道有没有关系?

环境:Windows 10; python 3.7

# -*- coding: utf-8 -*-

import os
from multiprocessing import Pool

p=0

def Child_process(id_number):
    global p
    print('Task start: %s(%s)' % (id_number, os.getpid()))
    print('p = %d' % p)
    p=p+1
    print('Task {} end'.format(id_number))


if __name__ == '__main__':
    p = Pool(4)
    p.map(Child_process,range(5))
    p.close()
    p.join()

结果是: 任务开始:0(7668)
p = 0
任务开始:1(10384)
任务0结束
p = 0
任务开始:2(7668)
p = 1
任务一结束
任务2结束
任务开始:3(7668)
任务开始:4(10384)
p = 1
任务4结束
p = 2
任务3结束

我认为 p 应该始终为 0,但是当不同的进程具有相同的 pid 时它会增加?

最佳答案

根据定义,线程/进程池将重复使用相同的线程/进程。这使您可以在线程/进程启动时设置资源,这样每个线程/进程就不必每次都初始化它们。这包括全局变量、打开的文件、套接字等。您可以通过将 initializer 函数传递给线程/进程来进行一次性初始化。因此,如果您设置或增加变量 p,它将在进程的各种运行过程中保持设置。如果您希望变量在每次运行时始终从 0 开始,则需要在每次运行开始时将其设置为 0。

此注释在 multiprocessing.pool.Pool 中类:

Note: Worker processes within a Pool typically live for the complete duration of the Pool’s work queue. A frequent pattern found in other systems (such as Apache, mod_wsgi, etc) to free resources held by workers is to allow a worker within a pool to complete only a set amount of work before being exiting, being cleaned up and a new process spawned to replace the old one. The maxtasksperchild argument to the Pool exposes this ability to the end user.

https://stackoverflow.com/questions/56990172/

相关文章:

reactjs - 如何将 MenuItem 用作 NavLink?

amazon-web-services - 为什么 Amazon Athena 会抛出 "Acces

laravel - 安装后如何使用 npm 包 (chart.js)?

assembly - 如何在 GDB 控制台中设置 "info registers"中的变量 rip

r - 从 R Survey 包运行 svymean 时,我可以得到 unwtd.count 吗?

python - 为什么 GridSearchCV 方法的精度低于标准方法?

django - 如何解决“"django.core.exceptions.AppRegistryN

node.js - 事件.js :180 throw er;//Unhandled 'error'

angular - 上传 PUT 请求由 Angular ServiceWorker 发送两次

angular - 自动滚动 onkeydown 即使没有必要