Reland "Port _FileDescriptorStreamsNonBlocking to use poll()"

Now that repo 2 requires Python 3, we can reland this.

This reverts commit 91d9587e45.

Change-Id: Id5b178ebb53bdba04bfa79cbb5c698ae5080c957
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/258672
Reviewed-by: Mike Frysinger <vapier@google.com>
Tested-by: Theodore Dubois <tbodt@google.com>
This commit is contained in:
Theodore Dubois 2020-03-12 01:06:57 +00:00
parent 8b40c00eab
commit 23d7dafd10

View File

@ -90,6 +90,11 @@ class _FileDescriptorStreamsNonBlocking(FileDescriptorStreams):
""" Implementation of FileDescriptorStreams for platforms that support """ Implementation of FileDescriptorStreams for platforms that support
non blocking I/O. non blocking I/O.
""" """
def __init__(self):
super(_FileDescriptorStreamsNonBlocking, self).__init__()
self._poll = select.poll()
self._fd_to_stream = {}
class Stream(object): class Stream(object):
""" Encapsulates a file descriptor """ """ Encapsulates a file descriptor """
@ -114,11 +119,18 @@ class _FileDescriptorStreamsNonBlocking(FileDescriptorStreams):
self.fd.close() self.fd.close()
def _create_stream(self, fd, dest, std_name): 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): def select(self):
ready_streams, _, _ = select.select(self.streams, [], []) return [self._fd_to_stream[fd] for fd, _ in self._poll.poll()]
return ready_streams
class _FileDescriptorStreamsThreads(FileDescriptorStreams): class _FileDescriptorStreamsThreads(FileDescriptorStreams):