简体   繁体   English

我确实希望每个多处理池工作者都有自己的全局变量副本并能够修改它

[英]I do want each multiprocessing pool worker to have its own copy of the global variable and be able to modify it

I want each process to have it's own copy of global variable and process should be able to modify and store the global variable:我希望每个进程都有自己的全局变量副本,并且进程应该能够修改和存储全局变量:

import multiprocessing
from multiprocessing import Pool
import time
input = [1,10]
first = 0
def printer (input) :
    global first
    first += input
    print('input', input)
    print('for input',input ,'first' , first)

def pool_handler():
    p = Pool(2)
    p.map(printer, input)


if __name__ == '__main__':
    while True:
        pool_handler()
        time.sleep(5)

My current output is我现在的 output 是

input 1
for input 1 first 1
input 10
for input 10 first 10 
input 1
for input 1 first 1
input 10
for input 10 first 10
...

WHere as my expected output is正如我预期的 output 是

input 1
for input 1 first 1
input 10
for input 10 first 10
input 1
for input 1 first 2
input 10
for input 10 first 20    

If we print process id, we can see each worker has its own variables:如果我们打印 process id,我们可以看到每个 worker 都有自己的变量:

import multiprocessing
from multiprocessing import Pool
import os
import time
input = [1,10]
first = 0
def printer (input) :
    global first
    first += input
    print(f'Process {os.getpid()} for input={input} first={first}')

def pool_handler(p):
    p.map(printer, input)

if __name__ == '__main__':
    p = Pool(2)
    while True:
        pool_handler(p)
        time.sleep(5)

I put pool allocation before the while loop.我将池分配放在 while 循环之前。

So It is not possible with multiprocessing.pool to create different processes where each process has it's own copy of global variable.因此 multiprocessing.pool 不可能创建不同的进程,其中每个进程都有自己的全局变量副本。 Since when pools are created all tasks are queued and and tasks are assigned to whichever pool is free.由于创建池时,所有任务都会排队,并且任务会分配给空闲的池。

So for my problem now i am using multiprocessing.process, I created two processes and now each has it's own copy of global variable, and each process can store and modify global variable.所以对于我的问题,我现在使用 multiprocessing.process,我创建了两个进程,现在每个进程都有自己的全局变量副本,每个进程都可以存储和修改全局变量。 Below i am posting my current code.下面我发布我当前的代码。

import multiprocessing
from multiprocessing import Pool,process,Process
import os
import time
#input = [1,10]
first = 0
def printer (input) :
    while True :
        global first
        first += input
        print(f'Process {os.getpid()} for input={input} first={first}')
        time.sleep(2)

if __name__ == '__main__':
    p1 = multiprocessing.Process(target=printer, args=(1,))
    p2 = multiprocessing.Process(target=printer, args=(10,))
        p1.start()
        p2.start()

and I had to move while True code in my function because once a process is terminated it can not restart the same process.我不得不移动我的 function 中的 True 代码,因为一旦进程终止,它就无法重新启动同一个进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM