pebble icon indicating copy to clipboard operation
pebble copied to clipboard

Allow a ProcessFuture access to actual process object.

Open ehudhala opened this issue 4 years ago • 3 comments

I have a need to wait for the concurrent process to finish, and there's currently no way to .join() the open process. Added a reference from the returned future to the actual process object.

ehudhala avatar Nov 21 '19 16:11 ehudhala

Hello,

you can actually wait directly on the Future object either by blocking on its results or using the generic wait function. In both cases you logic will be released exactly when the process finishes.

noxdafox avatar Nov 21 '19 19:11 noxdafox

Hello, thank you for the quick answer! In the current implementation this won't work, since for concurrent.process the process is only killed after the future is fulfilled.

The code for handling the future results in which the process gets killed only after the future is fulfilled:

def _worker_handler(future, worker, pipe, timeout):
    """Worker lifecycle manager.

    Waits for the worker to be perform its task,
    collects result, runs the callback and cleans up the process.

    """
    result = _get_result(future, pipe, timeout)

    if isinstance(result, BaseException):
        if isinstance(result, ProcessExpired):
            result.exitcode = worker.exitcode

        future.set_exception(result)
    else:
        future.set_result(result)

    if worker.is_alive():
        stop_process(worker)

Another option would be to move the process termination before the future fulfillment, but I don't know which effects this change will have.

ehudhala avatar Nov 24 '19 09:11 ehudhala

Please note your assumption is incorrect. The process terminates by itself when the _send_results function is called on its side. Therefore, when the _get_result is called and the Future results are set, the process is already gone.

The logic killing the process at the end of the function you pasted is simply a safeguard ensuring the process is joined (Process.is_alive has the side-effect of joining a dead process) and killed only if still alive for any reason.

When the Future callback is executed, the concurrent function you run through the decorator is long gone. You might still see the process in the process list because it has not been joined yet but it's a dead process.

noxdafox avatar Dec 01 '19 15:12 noxdafox

Closing this PR due to inactivity.

noxdafox avatar Sep 20 '22 12:09 noxdafox