From 23d7dafd10e382f42c34618f7c86b3ff1c9d4245 Mon Sep 17 00:00:00 2001 From: Theodore Dubois Date: Thu, 12 Mar 2020 01:06:57 +0000 Subject: [PATCH] Reland "Port _FileDescriptorStreamsNonBlocking to use poll()" Now that repo 2 requires Python 3, we can reland this. This reverts commit 91d9587e45608a5f95cd842426b43452a60abb5e. Change-Id: Id5b178ebb53bdba04bfa79cbb5c698ae5080c957 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/258672 Reviewed-by: Mike Frysinger Tested-by: Theodore Dubois --- platform_utils.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/platform_utils.py b/platform_utils.py index f1da75eb..d10e888d 100644 --- a/platform_utils.py +++ b/platform_utils.py @@ -90,6 +90,11 @@ class _FileDescriptorStreamsNonBlocking(FileDescriptorStreams): """ Implementation of FileDescriptorStreams for platforms that support non blocking I/O. """ + def __init__(self): + super(_FileDescriptorStreamsNonBlocking, self).__init__() + self._poll = select.poll() + self._fd_to_stream = {} + class Stream(object): """ Encapsulates a file descriptor """ @@ -114,11 +119,18 @@ class _FileDescriptorStreamsNonBlocking(FileDescriptorStreams): self.fd.close() def _create_stream(self, fd, dest, std_name): - return self.Stream(fd, dest, std_name) + stream = self.Stream(fd, dest, std_name) + self._fd_to_stream[stream.fileno()] = stream + self._poll.register(stream, select.POLLIN) + return stream + + def remove(self, stream): + self._poll.unregister(stream) + del self._fd_to_stream[stream.fileno()] + super(_FileDescriptorStreamsNonBlocking, self).remove(stream) def select(self): - ready_streams, _, _ = select.select(self.streams, [], []) - return ready_streams + return [self._fd_to_stream[fd] for fd, _ in self._poll.poll()] class _FileDescriptorStreamsThreads(FileDescriptorStreams):