8000 run the PidfdChildWatcher on the running loop · python/cpython@e49365f · GitHub
[go: up one dir, main page]

Skip to content

Commit e49365f

Browse files
committed
run the PidfdChildWatcher on the running loop
1 parent 4e796f5 commit e49365f

File tree

1 file changed

+12
-32
lines changed

1 file changed

+12
-32
lines changed

Lib/asyncio/unix_events.py

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -911,46 +911,29 @@ class PidfdChildWatcher(AbstractChildWatcher):
911911
recent (5.3+) kernels.
912912
"""
913913

914-
def __init__(self):
915-
self._loop = None
916-
self._callbacks = {}
917-
918914
def __enter__(self):
919915
return self
920916

921917
def __exit__(self, exc_type, exc_value, exc_traceback):
922918
pass
923919

924920
def is_active(self):
925-
return self._loop is not None and self._loop.is_running()
921+
return True
926922

927923
def close(self):
928-
self.attach_loop(None)
924+
pass
929925

930926
def attach_loop(self, loop):
931-
if self._loop is not None and loop is None and self._callbacks:
932-
warnings.warn(
933-
'A loop is being detached '
934-
'from a child watcher with pending handlers',
935-
RuntimeWarning)
936-
for pidfd, _, _ in self._callbacks.values():
937-
self._loop._remove_reader(pidfd)
938-
os.close(pidfd)
939-
self._callbacks.clear()
940-
self._loop = loop
927+
pass
941928

942929
def add_child_handler(self, pid, callback, *args):
943-
existing = self._callbacks.get(pid)
944-
if existing is not None:
945-
self._callbacks[pid] = existing[0], callback, args
946-
else:
947-
pidfd = os.pidfd_open(pid)
948-
self._loop._add_reader(pidfd, self._do_wait, pid)
949-
self._callbacks[pid] = pidfd, callback, args
930+
loop = events.get_running_loop()
931+
pidfd = os.pidfd_open(pid)
932+
loop._add_reader(pidfd, self._do_wait, pid, pidfd, callback, args)
950933

951-
def _do_wait(self, pid):
952-
pidfd, callback, args = self._callbacks.pop(pid)
953-
self._loop._remove_reader(pidfd)
934+
def _do_wait(self, pid, pidfd, callback, args):
935+
loop = events.get_running_loop()
936+
loop._remove_reader(pidfd)
954937
try:
955938
_, status = os.waitpid(pid, 0)
956939
except ChildProcessError:
@@ -968,12 +951,9 @@ def _do_wait(self, pid):
968951
callback(pid, returncode, *args)
969952

970953
def remove_child_handler(self, pid):
971-
try:
972-
pidfd, _, _ = self._callbacks.pop(pid)
973-
except KeyError:
974-
return False
975-
self._loop._remove_reader(pidfd)
976-
os.close(pidfd)
954+
# asyncio never calls remove_child_handler() !!!
955+
# The method is no-op but is implemented because
956+
# abstract base classes require it.
977957
return True
978958

979959

0 commit comments

Comments
 (0)
0