pollreactor.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. A poll() based implementation of the twisted main loop.
  5. To install the event loop (and you should do this before any connections,
  6. listeners or connectors are added)::
  7. from twisted.internet import pollreactor
  8. pollreactor.install()
  9. """
  10. from __future__ import division, absolute_import
  11. # System imports
  12. import errno
  13. from select import error as SelectError, poll
  14. from select import POLLIN, POLLOUT, POLLHUP, POLLERR, POLLNVAL
  15. from zope.interface import implementer
  16. # Twisted imports
  17. from twisted.python import log
  18. from twisted.internet import posixbase
  19. from twisted.internet.interfaces import IReactorFDSet
  20. @implementer(IReactorFDSet)
  21. class PollReactor(posixbase.PosixReactorBase, posixbase._PollLikeMixin):
  22. """
  23. A reactor that uses poll(2).
  24. @ivar _poller: A L{select.poll} which will be used to check for I/O
  25. readiness.
  26. @ivar _selectables: A dictionary mapping integer file descriptors to
  27. instances of L{FileDescriptor} which have been registered with the
  28. reactor. All L{FileDescriptor}s which are currently receiving read or
  29. write readiness notifications will be present as values in this
  30. dictionary.
  31. @ivar _reads: A dictionary mapping integer file descriptors to arbitrary
  32. values (this is essentially a set). Keys in this dictionary will be
  33. registered with C{_poller} for read readiness notifications which will
  34. be dispatched to the corresponding L{FileDescriptor} instances in
  35. C{_selectables}.
  36. @ivar _writes: A dictionary mapping integer file descriptors to arbitrary
  37. values (this is essentially a set). Keys in this dictionary will be
  38. registered with C{_poller} for write readiness notifications which will
  39. be dispatched to the corresponding L{FileDescriptor} instances in
  40. C{_selectables}.
  41. """
  42. _POLL_DISCONNECTED = (POLLHUP | POLLERR | POLLNVAL)
  43. _POLL_IN = POLLIN
  44. _POLL_OUT = POLLOUT
  45. def __init__(self):
  46. """
  47. Initialize polling object, file descriptor tracking dictionaries, and
  48. the base class.
  49. """
  50. self._poller = poll()
  51. self._selectables = {}
  52. self._reads = {}
  53. self._writes = {}
  54. posixbase.PosixReactorBase.__init__(self)
  55. def _updateRegistration(self, fd):
  56. """Register/unregister an fd with the poller."""
  57. try:
  58. self._poller.unregister(fd)
  59. except KeyError:
  60. pass
  61. mask = 0
  62. if fd in self._reads:
  63. mask = mask | POLLIN
  64. if fd in self._writes:
  65. mask = mask | POLLOUT
  66. if mask != 0:
  67. self._poller.register(fd, mask)
  68. else:
  69. if fd in self._selectables:
  70. del self._selectables[fd]
  71. def _dictRemove(self, selectable, mdict):
  72. try:
  73. # the easy way
  74. fd = selectable.fileno()
  75. # make sure the fd is actually real. In some situations we can get
  76. # -1 here.
  77. mdict[fd]
  78. except:
  79. # the hard way: necessary because fileno() may disappear at any
  80. # moment, thanks to python's underlying sockets impl
  81. for fd, fdes in self._selectables.items():
  82. if selectable is fdes:
  83. break
  84. else:
  85. # Hmm, maybe not the right course of action? This method can't
  86. # fail, because it happens inside error detection...
  87. return
  88. if fd in mdict:
  89. del mdict[fd]
  90. self._updateRegistration(fd)
  91. def addReader(self, reader):
  92. """Add a FileDescriptor for notification of data available to read.
  93. """
  94. fd = reader.fileno()
  95. if fd not in self._reads:
  96. self._selectables[fd] = reader
  97. self._reads[fd] = 1
  98. self._updateRegistration(fd)
  99. def addWriter(self, writer):
  100. """Add a FileDescriptor for notification of data available to write.
  101. """
  102. fd = writer.fileno()
  103. if fd not in self._writes:
  104. self._selectables[fd] = writer
  105. self._writes[fd] = 1
  106. self._updateRegistration(fd)
  107. def removeReader(self, reader):
  108. """Remove a Selectable for notification of data available to read.
  109. """
  110. return self._dictRemove(reader, self._reads)
  111. def removeWriter(self, writer):
  112. """Remove a Selectable for notification of data available to write.
  113. """
  114. return self._dictRemove(writer, self._writes)
  115. def removeAll(self):
  116. """
  117. Remove all selectables, and return a list of them.
  118. """
  119. return self._removeAll(
  120. [self._selectables[fd] for fd in self._reads],
  121. [self._selectables[fd] for fd in self._writes])
  122. def doPoll(self, timeout):
  123. """Poll the poller for new events."""
  124. if timeout is not None:
  125. timeout = int(timeout * 1000) # convert seconds to milliseconds
  126. try:
  127. l = self._poller.poll(timeout)
  128. except SelectError as e:
  129. if e.args[0] == errno.EINTR:
  130. return
  131. else:
  132. raise
  133. _drdw = self._doReadOrWrite
  134. for fd, event in l:
  135. try:
  136. selectable = self._selectables[fd]
  137. except KeyError:
  138. # Handles the infrequent case where one selectable's
  139. # handler disconnects another.
  140. continue
  141. log.callWithLogger(selectable, _drdw, selectable, fd, event)
  142. doIteration = doPoll
  143. def getReaders(self):
  144. return [self._selectables[fd] for fd in self._reads]
  145. def getWriters(self):
  146. return [self._selectables[fd] for fd in self._writes]
  147. def install():
  148. """Install the poll() reactor."""
  149. p = PollReactor()
  150. from twisted.internet.main import installReactor
  151. installReactor(p)
  152. __all__ = ["PollReactor", "install"]