selectreactor.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # -*- test-case-name: twisted.test.test_internet -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Select reactor
  6. """
  7. from __future__ import division, absolute_import
  8. from time import sleep
  9. import sys, select, socket
  10. from errno import EINTR, EBADF
  11. from zope.interface import implementer
  12. from twisted.internet.interfaces import IReactorFDSet
  13. from twisted.internet import posixbase
  14. from twisted.python import log
  15. from twisted.python.runtime import platformType
  16. def win32select(r, w, e, timeout=None):
  17. """Win32 select wrapper."""
  18. if not (r or w):
  19. # windows select() exits immediately when no sockets
  20. if timeout is None:
  21. timeout = 0.01
  22. else:
  23. timeout = min(timeout, 0.001)
  24. sleep(timeout)
  25. return [], [], []
  26. # windows doesn't process 'signals' inside select(), so we set a max
  27. # time or ctrl-c will never be recognized
  28. if timeout is None or timeout > 0.5:
  29. timeout = 0.5
  30. r, w, e = select.select(r, w, w, timeout)
  31. return r, w + e, []
  32. if platformType == "win32":
  33. _select = win32select
  34. else:
  35. _select = select.select
  36. try:
  37. from twisted.internet.win32eventreactor import _ThreadedWin32EventsMixin
  38. except ImportError:
  39. _extraBase = object
  40. else:
  41. _extraBase = _ThreadedWin32EventsMixin
  42. @implementer(IReactorFDSet)
  43. class SelectReactor(posixbase.PosixReactorBase, _extraBase):
  44. """
  45. A select() based reactor - runs on all POSIX platforms and on Win32.
  46. @ivar _reads: A set containing L{FileDescriptor} instances which will be
  47. checked for read events.
  48. @ivar _writes: A set containing L{FileDescriptor} instances which will be
  49. checked for writability.
  50. """
  51. def __init__(self):
  52. """
  53. Initialize file descriptor tracking dictionaries and the base class.
  54. """
  55. self._reads = set()
  56. self._writes = set()
  57. posixbase.PosixReactorBase.__init__(self)
  58. def _preenDescriptors(self):
  59. log.msg("Malformed file descriptor found. Preening lists.")
  60. readers = list(self._reads)
  61. writers = list(self._writes)
  62. self._reads.clear()
  63. self._writes.clear()
  64. for selSet, selList in ((self._reads, readers),
  65. (self._writes, writers)):
  66. for selectable in selList:
  67. try:
  68. select.select([selectable], [selectable], [selectable], 0)
  69. except Exception as e:
  70. log.msg("bad descriptor %s" % selectable)
  71. self._disconnectSelectable(selectable, e, False)
  72. else:
  73. selSet.add(selectable)
  74. def doSelect(self, timeout):
  75. """
  76. Run one iteration of the I/O monitor loop.
  77. This will run all selectables who had input or output readiness
  78. waiting for them.
  79. """
  80. try:
  81. r, w, ignored = _select(self._reads,
  82. self._writes,
  83. [], timeout)
  84. except ValueError:
  85. # Possibly a file descriptor has gone negative?
  86. self._preenDescriptors()
  87. return
  88. except TypeError:
  89. # Something *totally* invalid (object w/o fileno, non-integral
  90. # result) was passed
  91. log.err()
  92. self._preenDescriptors()
  93. return
  94. except (select.error, socket.error, IOError) as se:
  95. # select(2) encountered an error, perhaps while calling the fileno()
  96. # method of a socket. (Python 2.6 socket.error is an IOError
  97. # subclass, but on Python 2.5 and earlier it is not.)
  98. if se.args[0] in (0, 2):
  99. # windows does this if it got an empty list
  100. if (not self._reads) and (not self._writes):
  101. return
  102. else:
  103. raise
  104. elif se.args[0] == EINTR:
  105. return
  106. elif se.args[0] == EBADF:
  107. self._preenDescriptors()
  108. return
  109. else:
  110. # OK, I really don't know what's going on. Blow up.
  111. raise
  112. _drdw = self._doReadOrWrite
  113. _logrun = log.callWithLogger
  114. for selectables, method, fdset in ((r, "doRead", self._reads),
  115. (w,"doWrite", self._writes)):
  116. for selectable in selectables:
  117. # if this was disconnected in another thread, kill it.
  118. # ^^^^ --- what the !@#*? serious! -exarkun
  119. if selectable not in fdset:
  120. continue
  121. # This for pausing input when we're not ready for more.
  122. _logrun(selectable, _drdw, selectable, method)
  123. doIteration = doSelect
  124. def _doReadOrWrite(self, selectable, method):
  125. try:
  126. why = getattr(selectable, method)()
  127. except:
  128. why = sys.exc_info()[1]
  129. log.err()
  130. if why:
  131. self._disconnectSelectable(selectable, why, method=="doRead")
  132. def addReader(self, reader):
  133. """
  134. Add a FileDescriptor for notification of data available to read.
  135. """
  136. self._reads.add(reader)
  137. def addWriter(self, writer):
  138. """
  139. Add a FileDescriptor for notification of data available to write.
  140. """
  141. self._writes.add(writer)
  142. def removeReader(self, reader):
  143. """
  144. Remove a Selectable for notification of data available to read.
  145. """
  146. self._reads.discard(reader)
  147. def removeWriter(self, writer):
  148. """
  149. Remove a Selectable for notification of data available to write.
  150. """
  151. self._writes.discard(writer)
  152. def removeAll(self):
  153. return self._removeAll(self._reads, self._writes)
  154. def getReaders(self):
  155. return list(self._reads)
  156. def getWriters(self):
  157. return list(self._writes)
  158. def install():
  159. """Configure the twisted mainloop to be run using the select() reactor.
  160. """
  161. reactor = SelectReactor()
  162. from twisted.internet.main import installReactor
  163. installReactor(reactor)
  164. __all__ = ['install']