Patch mp procs with a `.poll()`

Not sure why they don't already expose this from the `Popen` backends
but, k.
maybe_cancel_the_cancel_
Tyler Goodlet 2021-12-22 14:53:36 -05:00
parent a2171c7e71
commit b1d72b77c9
1 changed files with 11 additions and 3 deletions

View File

@ -82,14 +82,16 @@ else:
def try_set_start_method(name: str) -> Optional[mp.context.BaseContext]: def try_set_start_method(name: str) -> Optional[mp.context.BaseContext]:
"""Attempt to set the method for process starting, aka the "actor '''
Attempt to set the method for process starting, aka the "actor
spawning backend". spawning backend".
If the desired method is not supported this function will error. If the desired method is not supported this function will error.
On Windows only the ``multiprocessing`` "spawn" method is offered On Windows only the ``multiprocessing`` "spawn" method is offered
besides the default ``trio`` which uses async wrapping around besides the default ``trio`` which uses async wrapping around
``subprocess.Popen``. ``subprocess.Popen``.
"""
'''
global _ctx global _ctx
global _spawn_method global _spawn_method
@ -243,7 +245,7 @@ async def soft_wait(
n.start_soon(cancel_on_proc_deth) n.start_soon(cancel_on_proc_deth)
await portal.cancel_actor() await portal.cancel_actor()
if proc.poll() is None: if proc.poll() is None: # type: ignore
log.warning( log.warning(
f'Process still alive after cancel request:\n{uid}') f'Process still alive after cancel request:\n{uid}')
@ -502,6 +504,7 @@ async def mp_new_proc(
# daemon=True, # daemon=True,
name=name, name=name,
) )
# `multiprocessing` only (since no async interface): # `multiprocessing` only (since no async interface):
# register the process before start in case we get a cancel # register the process before start in case we get a cancel
# request before the actor has fully spawned - then we can wait # request before the actor has fully spawned - then we can wait
@ -520,6 +523,11 @@ async def mp_new_proc(
# local actor by the time we get a ref to it # local actor by the time we get a ref to it
event, chan = await actor_nursery._actor.wait_for_peer( event, chan = await actor_nursery._actor.wait_for_peer(
subactor.uid) subactor.uid)
# XXX: monkey patch poll API to match the ``subprocess`` API..
# not sure why they don't expose this but kk.
proc.poll = proc._popen.poll # type: ignore
# except: # except:
# TODO: in the case we were cancelled before the sub-proc # TODO: in the case we were cancelled before the sub-proc
# registered itself back we must be sure to try and clean # registered itself back we must be sure to try and clean