diff options
author | Helmut Grohne <helmut@subdivi.de> | 2024-05-20 12:01:59 +0200 |
---|---|---|
committer | Helmut Grohne <helmut@subdivi.de> | 2024-05-20 12:01:59 +0200 |
commit | 992e877614476dd40abd11a82ffedc6e261dabdf (patch) | |
tree | d4146decb33070788264aadfb2dcdd22a14ac419 | |
parent | 242a541f0f31f6eef702317d9247fd050a077c85 (diff) | |
download | python-linuxnamespaces-992e877614476dd40abd11a82ffedc6e261dabdf.tar.gz |
add an asyncio waitid(P_PIDFD, ...) helper
-rw-r--r-- | linuxnamespaces/__init__.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/linuxnamespaces/__init__.py b/linuxnamespaces/__init__.py index 1a8b0ee..3302867 100644 --- a/linuxnamespaces/__init__.py +++ b/linuxnamespaces/__init__.py @@ -673,3 +673,37 @@ def async_copyfd( ): return _AsyncSplicer(from_fd, to_fd, count).fut return _AsyncCopier(from_fd, to_fd, count).fut + + +class _AsyncPidfdWaiter: + def __init__(self, pidfd: int, flags: int): + self.pidfd = pidfd + self.flags = flags + self.loop = asyncio.get_running_loop() + self.fut: asyncio.Future[ + os.waitid_result | None + ] = self.loop.create_future() + self.loop.add_reader(pidfd, self.handle_readable) + + def handle_readable(self) -> None: + try: + result = os.waitid(os.P_PIDFD, self.pidfd, self.flags) + except OSError as err: + if err.errno != errno.EAGAIN: + self.loop.remove_reader(self.pidfd) + self.fut.set_exception(err) + except Exception as err: + self.loop.remove_reader(self.pidfd) + self.fut.set_exception(err) + else: + self.loop.remove_reader(self.pidfd) + self.fut.set_result(result) + + +def async_waitpidfd( + pidfd: int, flags: int +) -> asyncio.Future[os.waitid_result | None]: + """Asynchronously wait for a process represented as a pidfd. This is an + async variant of waitid(P_PIDFD, pidfd, flags). + """ + return _AsyncPidfdWaiter(pidfd, flags).fut |