Retrieve exit code of processes launched with multiprocessing.Pool.map

后端 未结 1 2095
长发绾君心
长发绾君心 2020-12-18 03:49

I\'m using python multiprocessing module to parallelize some computationally heavy tasks. The obvious choice is to use a Pool of workers and then

相关标签:
1条回答
  • 2020-12-18 04:08

    If you're using multiprocessing.Pool.map you're generally not interested in the exit code of the sub-processes in the pool, you're interested in what value they returned from their work item. This is because under normal conditions, the processes in a Pool won't exit until you close/join the pool, so there's no exit codes to retrieve until all work is complete, and the Pool is about to be destroyed. Because of this, there is no public API to get the exit codes of those sub-processes.

    Now, you're worried about exceptional conditions, where something out-of-band kills one of the sub-processes while it's doing work. If you hit an issue like this, you're probably going to run into some strange behavior. In fact, in my tests where I killed a process in a Pool while it was doing work as part of a map call, map never completed, because the killed process didn't complete. Python did, however, immediately launch a new process to replace the one I killed.

    That said, you can get the pid of each process in your pool by accessing the multiprocessing.Process objects inside the pool directly, using the private _pool attribute:

    pool = multiprocessing.Pool()
    for proc in pool._pool:
      print proc.pid
    

    So, one thing you could do to try to detect when a process had died unexpectedly (assuming you don't get stuck in a blocking call as a result). You can do this by examining the list of processes in the pool before and after making a call to map_async:

    before = pool._pool[:]  # Make a copy of the list of Process objects in our pool
    result = pool.map_async(func, iterable)  # Use map_async so we don't get stuck.
    while not result.ready():  # Wait for the call to complete
        if any(proc.exitcode for proc in before):  # Abort if one of our original processes is dead.
            print "One of our processes has exited. Something probably went horribly wrong."
            break
        result.wait(timeout=1)
    else:  # We'll enter this block if we don't reach `break` above.
        print result.get() # Actually fetch the result list here.
    

    We have to make a copy of the list because when a process in the Pool dies, Python immediately replaces it with a new process, and removes the dead one from the list.

    This worked for me in my tests, but because it's relying on a private attribute of the Pool object (_pool) it's risky to use in production code. I would also suggest that it may be overkill to worry too much about this scenario, since it's very unlikely to occur and complicates the implementation significantly.

    0 讨论(0)
提交回复
热议问题