简体   繁体   English

如何使用 multiprocessing.Pool 判断 apply_async 函数是否已启动或仍在队列中

[英]How to tell if an apply_async function has started or if it's still in the queue with multiprocessing.Pool

I'm using python's multiprocessing.Pool and apply_async to call a bunch of functions.我正在使用 python 的 multiprocessing.Pool 和 apply_async 来调用一堆函数。

How can I tell whether a function has started processing by a member of the pool or whether it is sitting in a queue?如何判断函数是否已由池成员开始处理,或者它是否处于队列中?

For example:例如:

import multiprocessing
import time

def func(t):
    #take some time processing
    print 'func({}) started'.format(t)
    time.sleep(t)

pool = multiprocessing.Pool()

results = [pool.apply_async(func, [t]) for t in [100]*50] #adds 50 func calls to the queue

For each AsyncResult in results you can call ready() or get(0) to see if the func finished running.对于results每个AsyncResult ,您可以调用ready()get(0)以查看 func 是否已完成运行。 But how do you find out whether the func started but hasn't finished yet?但是你怎么知道 func 是否启动了但还没有完成呢?

ie for a given AsyncResult object (ie a given element of results) is there a way to see whether the function has been called or if it's sitting in the pool's queue?即对于给定的 AsyncResult 对象(即给定的结果元素),有没有办法查看该函数是否已被调用,或者它是否位于池的队列中?

First, remove completed jobs from results list首先,从结果列表中删除已完成的作业

    results = [r for r in results if not r.ready()]

Number of processes pending is length of results list:待处理的进程数是结果列表的长度:

    pending = len(results)

And number pending but not started is total pending - pool_size待处理但未启动的数量是待处理的总数 - pool_size

    not_started = pending - pool_size

pool_size will be multiprocessing.cpu_count() if Pool is created with default argument as you did如果 Pool 是使用默认参数创建的,则 pool_size 将是 multiprocessing.cpu_count()

UPDATE : After initially misunderstanding the question, here's a way to do what OP was asking about.更新:在最初误解了这个问题之后,这里有一种方法可以做 OP 所问的问题。

I suspect this functionality could be added to the Pool class without too much trouble because AsyncResult is implemented by Pool with a Queue.我怀疑这个功能可以添加到 Pool 类中而不会有太多麻烦,因为 AsyncResult 是由带有队列的 Pool 实现的。 That queue could also be used internally to indicate whether started or not.该队列也可以在内部使用以指示是否已启动。

But here's a way to implement using Pool and Pipe.但是这里有一种使用 Pool 和 Pipe 来实现的方法。 NOTE: this doesn't work in Python 2.x -- not sure why.注意:这在 Python 2.x 中不起作用——不知道为什么。 Tested in Python 3.8.在 Python 3.8 中测试。

import multiprocessing
import time
import os

def worker_function(pipe):
    pipe.send('started')
    print('[{}] started pipe={}'.format(os.getpid(), pipe))
    time.sleep(3)
    pipe.close()

def test():
    pool = multiprocessing.Pool(processes=2)
    print('[{}] pool={}'.format(os.getpid(), pool))

    workers = []

    for x in range(1, 4):
        parent, child = multiprocessing.Pipe()
        pool.apply_async(worker_function, (child,))
        worker = {'name': 'worker{}'.format(x), 'pipe': parent, 'started': False}
        workers.append(worker)

    pool.close()

    while True:
        for worker in workers:
            if worker.get('started'):
                continue
            pipe = worker.get('pipe')
            if pipe.poll(0.1):
                message = pipe.recv()
                print('[{}] {} says {}'.format(os.getpid(), worker.get('name'), message))
                worker['started'] = True
                pipe.close()
        count_in_queue = len(workers)
        for worker in workers:
            if worker.get('started'):
                count_in_queue -= 1
        print('[{}] count_in_queue = {}'.format(os.getpid(), count_in_queue))
        if not count_in_queue:
            break
        time.sleep(0.5)

    pool.join()

if __name__ == '__main__':
    test()

暂无
暂无

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

相关问题 如何将multiprocessing.Pool实例传递给apply_async回调函数? - How to pass multiprocessing.Pool instance to apply_async callback function? multiprocessing.Pool:何时使用 apply、apply_async 或 map? - multiprocessing.Pool: When to use apply, apply_async or map? 在multiprocessing中有error_callback。在Python 2中有池apply_async吗? - error_callback in multiprocessing.Pool apply_async in Python 2? 多处理中的工作者是否有办法.Pool的apply_async可以捕获错误并继续? - Is there a way for workers in multiprocessing.Pool's apply_async to catch errors and continue? multiprocessing.Pool:使用apply_async的回调选项时调用辅助函数 - multiprocessing.Pool: calling helper functions when using apply_async's callback option multiprocessing.Pool().apply_async() 似乎没有运行我的函数 - multiprocessing.Pool().apply_async() doesn't seem to run my function 当我从multiprocessing.Pool调用apply_async时,为什么会抛出“'module'对象没有属性XXX”错误? - Why would it throws “'module' object has no attribute XXX” error when I call on apply_async from multiprocessing.Pool? 多处理池apply_async - Multiprocessing pool apply_async 为什么在multiprocessing.Pool()。apply_async()中使用了多个工人? - why is more than one worker used in `multiprocessing.Pool().apply_async()`? 为什么我的 multiprocessing.Pool apply_async 只在 for 循环中执行一次 - Why is my multiprocessing.Pool apply_async only executed once inside a for loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM