使用python多处理模块递增/递减全局变量

时间:2021-04-13 23:13:45

I am trying to launch multiple processes to parallelize certain tasks and want one global variable to be decremented by 1 each time each process executes a method X().

我试图启动多个进程来并行化某些任务,并希望每个进程执行一个方法X()时,一个全局变量减1。

I tried to look at the multiprocessing.Value method but not sure if that's the only way to do it. Could someone provide some code snippets to do this ?

我试着看看multiprocessing.Value方法,但不确定这是否是唯一的方法。有人可以提供一些代码片段来做到这一点吗?

from multiprocessing import Pool, Process

def X(list):
  global temp
  print list
  temp = 10
  temp -= 1
  return temp

list = ['a','b','c']
pool = Pool(processes=5)
pool.map(X, list)

With the use of global variable, each process gets its own copy of the global variable which doesn't solve the purpose of sharing it's value. I believe, the need is to have sort of a shared memory system but I am not sure how to do it. Thanks

通过使用全局变量,每个进程都获得自己的全局变量副本,但这并不能解决共享它的价值的问题。我相信,需要有一种共享内存系统,但我不知道该怎么做。谢谢

1 个解决方案

#1


1  

Move counter variable into the main process i.e., avoid sharing the variable between processes:

将计数器变量移动到主进程中,即避免在进程之间共享变量:

for result in pool.imap_unordered(func, args):
    counter -= 1

counter is decremented as soon as the corresponding result (func(arg)) becomes available. Here's a complete code example:

一旦相应的结果(func(arg))变得可用,计数器就会递减。这是一个完整的代码示例:

#!/usr/bin/env python
import random
import time
import multiprocessing

def func(arg):
    time.sleep(random.random())
    return arg*10

def main():
    counter = 10
    args = "abc"
    pool = multiprocessing.Pool()
    for result in pool.imap_unordered(func, args):
        counter -= 1
        print("counter=%d, result=%r" % (counter, result))

if __name__ == "__main__":
    main()

An alternative is to pass multiprocessing.Value() object to each worker process (use initialize, initargs Pool()'s parameters).

另一种方法是将multiprocessing.Value()对象传递给每个工作进程(使用initialize,initargs Pool()的参数)。

#1


1  

Move counter variable into the main process i.e., avoid sharing the variable between processes:

将计数器变量移动到主进程中,即避免在进程之间共享变量:

for result in pool.imap_unordered(func, args):
    counter -= 1

counter is decremented as soon as the corresponding result (func(arg)) becomes available. Here's a complete code example:

一旦相应的结果(func(arg))变得可用,计数器就会递减。这是一个完整的代码示例:

#!/usr/bin/env python
import random
import time
import multiprocessing

def func(arg):
    time.sleep(random.random())
    return arg*10

def main():
    counter = 10
    args = "abc"
    pool = multiprocessing.Pool()
    for result in pool.imap_unordered(func, args):
        counter -= 1
        print("counter=%d, result=%r" % (counter, result))

if __name__ == "__main__":
    main()

An alternative is to pass multiprocessing.Value() object to each worker process (use initialize, initargs Pool()'s parameters).

另一种方法是将multiprocessing.Value()对象传递给每个工作进程(使用initialize,initargs Pool()的参数)。