gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 07:21:18 32 4
gpt4 key购买 nike

我不熟悉多处理模块。我正在尝试验证不同过程中的变量是否无关紧要。经过测试,我发现不同的进程很可能“共享”变量。当进程具有相同的 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.

关于python - 在多处理模块中使用 Pool 修改全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56990172/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com