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
| 855 | |
| 856 | |
| 857 | class _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 | |
| 893 | class _ThreadedChildWatcher: |
| 894 | """Threaded child watcher implementation. |
no outgoing calls
no test coverage detected
searching dependent graphs…