MCPcopy Index your code
hub / github.com/python/cpython / _PidfdChildWatcher

Class _PidfdChildWatcher

Lib/asyncio/unix_events.py:857–891  ·  view source on GitHub ↗

Child watcher implementation using Linux's pid file descriptors. This child watcher polls process file descriptors (pidfds) to await child process termination. In some respects, PidfdChildWatcher is a "Goldilocks" child watcher implementation. It doesn't require signals or threads, does

Source from the content-addressed store, hash-verified

855
856
857class _PidfdChildWatcher:
858 """Child watcher implementation using Linux's pid file descriptors.
859
860 This child watcher polls process file descriptors (pidfds) to await child
861 process termination. In some respects, PidfdChildWatcher is a "Goldilocks"
862 child watcher implementation. It doesn't require signals or threads, doesn't
863 interfere with any processes launched outside the event loop, and scales
864 linearly with the number of subprocesses launched by the event loop. The
865 main disadvantage is that pidfds are specific to Linux, and only work on
866 recent (5.3+) kernels.
867 """
868
869 def add_child_handler(self, pid, callback, *args):
870 loop = events.get_running_loop()
871 pidfd = os.pidfd_open(pid)
872 loop._add_reader(pidfd, self._do_wait, pid, pidfd, callback, args)
873
874 def _do_wait(self, pid, pidfd, callback, args):
875 loop = events.get_running_loop()
876 loop._remove_reader(pidfd)
877 try:
878 _, status = os.waitpid(pid, 0)
879 except ChildProcessError:
880 # The child process is already reaped
881 # (may happen if waitpid() is called elsewhere).
882 returncode = 255
883 logger.warning(
884 "child process pid %d exit status already read: "
885 " will report returncode 255",
886 pid)
887 else:
888 returncode = waitstatus_to_exitcode(status)
889
890 os.close(pidfd)
891 callback(pid, returncode, *args)
892
893class _ThreadedChildWatcher:
894 """Threaded child watcher implementation.

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…