Skip to content

Commit

Permalink
Fix asyncio child_watcher for Python 3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Oct 28, 2022
1 parent 532e3ca commit b3aae5e
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions gobexec/asyncio/child_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@
import resource
from asyncio import ThreadedChildWatcher
from asyncio.log import logger
from asyncio.unix_events import _compute_returncode

# based on https://www.enricozini.org/blog/2019/debian/getting-rusage-of-child-processes-on-python-s-asyncio/
from typing import Dict


# copied from unix_events.ThreadedChildWatcher on Python 3.10.6
def waitstatus_to_exitcode(status):
try:
return os.waitstatus_to_exitcode(status)
except ValueError:
# The child exited, but we don't understand its status.
# This shouldn't happen, but if it does, let's just
# return that status; perhaps that helps debug it.
return status


class RusageThreadedChildWatcher(ThreadedChildWatcher):
rusages: Dict[int, resource.struct_rusage] = {}

# copied from unix_events.ThreadedChildWatcher on Python 3.9.7
# copied from unix_events.ThreadedChildWatcher on Python 3.10.6
def _do_waitpid(self, loop, expected_pid, callback, args):
assert expected_pid > 0

Expand All @@ -27,7 +37,7 @@ def _do_waitpid(self, loop, expected_pid, callback, args):
"Unknown child process pid %d, will report returncode 255",
pid)
else:
returncode = _compute_returncode(status)
returncode = waitstatus_to_exitcode(status)
if loop.get_debug():
logger.debug('process %s exited with returncode %s',
expected_pid, returncode)
Expand Down

0 comments on commit b3aae5e

Please sign in to comment.