如何度量python多处理映射异步的进度?

如何度量python多处理映射异步的进度?,python,multiprocessing,python-multiprocessing,Python,Multiprocessing,Python Multiprocessing,我曾经使用这个片段来监视多进程的进程。但是,现在q.qsize()停止工作,只返回零。有没有替代以前使用的q.qsize()功能?还是有更好的方法 from multiprocessing import Pool, Manager, cpu_count def process(args): ''' Pickleable function for (parallel) processing. Expects a dictionary with keys:

我曾经使用这个片段来监视多进程的进程。但是,现在q.qsize()停止工作,只返回零。有没有替代以前使用的
q.qsize()
功能?还是有更好的方法

from multiprocessing import Pool, Manager, cpu_count

def process(args):
    '''
    Pickleable function for (parallel) processing.
    Expects a dictionary with keys:
        Mandatory:
        - 'filename': 'path to file to be processed',
        ...
        Optional:
        - 'queue': instance of multiprocessing.Manager().Queue()
    '''
    filename = args['filename']
    mode = args['mode']

    if 'queue' in args.keys():
        q = args['queue']
        q.put('filename')
return do_something_with(filename)
...


pool = Pool(processes=nthreads)
m = Manager()
q = m.Queue()

# prepare args...

results = pool.map_async(process, args)

# monitor progress
while True:
    if results.ready():
        break
    else:
        size = q.qsize()
        self.progress = int(100 * (size / self.n_files))
        time.sleep(1)
self.progress = 100

看起来你把你的池和队列搞混了。多处理池类为您处理队列逻辑,但该池不是队列对象,而是池。因此,当您检查队列大小时,根本没有任何内容

不过,为了直接回答您的问题,您的
q.qsize()
始终将为0,因为您实际上没有将任何东西排队。您的队列总是空的。您需要
q.put()
队列中的某些内容,如果这是您正在寻找的行为


据我所知,没有任何东西能与您的代码完全兼容,因为您现在使用的是
pool.map\u async
。我不确定您想从这里如何处理它,但是关于检查池中剩余的项目有很多

事实上,我认为这是可行的。我的代码中有一个bug。我在这里补充了一些代码。但这可能不是最好的方法。其目的是在串行和并行情况下使用这个过程。哎呀,有一些代码丢失了,我本应该发布的。谢谢你指出这一点。