How to increment a shared counter from multiple processes?(如何从多个进程递增共享计数器?)
                            本文介绍了如何从多个进程递增共享计数器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
                        
                        问题描述
我在使用multiprocessing模块时遇到问题。我使用Pool个工作者及其map方法并发分析大量文件。每次处理文件时,我都希望更新计数器,这样我就可以跟踪还有多少文件需要处理。以下是示例代码:
import os
import multiprocessing
counter = 0
def analyze(file):
    # Analyze the file.
    global counter
    counter += 1
    print counter
if __name__ == '__main__':
    files = os.listdir('/some/directory')
    pool = multiprocessing.Pool(4)
    pool.map(analyze, files)
我找不到此问题的解决方案。
推荐答案
问题在于counter变量不在您的进程之间共享:每个单独的进程都在创建自己的本地实例并递增该实例。
Value实例
这里是您的示例的工作版本(带有一些虚拟输入数据)。请注意,它使用的是我在实践中确实会尽量避免的全局值:
from multiprocessing import Pool, Value
from time import sleep
counter = None
def init(args):
    ''' store the counter for later use '''
    global counter
    counter = args
def analyze_data(args):
    ''' increment the global counter, do something with the input '''
    global counter
    # += operation is not atomic, so we need to get a lock:
    with counter.get_lock():
        counter.value += 1
    print counter.value
    return args * 10
if __name__ == '__main__':
    #inputs = os.listdir(some_directory)
    #
    # initialize a cross-process counter and the input lists
    #
    counter = Value('i', 0)
    inputs = [1, 2, 3, 4]
    #
    # create the pool of workers, ensuring each one receives the counter 
    # as it starts. 
    #
    p = Pool(initializer = init, initargs = (counter, ))
    i = p.map_async(analyze_data, inputs, chunksize = 1)
    i.wait()
    print i.get()
                        这篇关于如何从多个进程递增共享计数器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
				 沃梦达教程
				
			本文标题为:如何从多个进程递增共享计数器?
				
        
 
            
        基础教程推荐
             猜你喜欢
        
	     - Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
 - 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
 - 修改列表中的数据帧不起作用 2022-01-01
 - PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
 - 包装空间模型 2022-01-01
 - 在Python中从Azure BLOB存储中读取文件 2022-01-01
 - 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
 - PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
 - 求两个直方图的卷积 2022-01-01
 - 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				