default.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- test-case-name: twisted.internet.test.test_default -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. The most suitable default reactor for the current platform.
  6. Depending on a specific application's needs, some other reactor may in
  7. fact be better.
  8. """
  9. from __future__ import division, absolute_import
  10. __all__ = ["install"]
  11. from twisted.python.runtime import platform
  12. def _getInstallFunction(platform):
  13. """
  14. Return a function to install the reactor most suited for the given platform.
  15. @param platform: The platform for which to select a reactor.
  16. @type platform: L{twisted.python.runtime.Platform}
  17. @return: A zero-argument callable which will install the selected
  18. reactor.
  19. """
  20. # Linux: epoll(7) is the default, since it scales well.
  21. #
  22. # macOS: poll(2) is not exposed by Python because it doesn't support all
  23. # file descriptors (in particular, lack of PTY support is a problem) --
  24. # see <http://bugs.python.org/issue5154>. kqueue has the same restrictions
  25. # as poll(2) as far PTY support goes.
  26. #
  27. # Windows: IOCP should eventually be default, but still has some serious
  28. # bugs, e.g. <http://twistedmatrix.com/trac/ticket/4667>.
  29. #
  30. # We therefore choose epoll(7) on Linux, poll(2) on other non-macOS POSIX
  31. # platforms, and select(2) everywhere else.
  32. try:
  33. if platform.isLinux():
  34. try:
  35. from twisted.internet.epollreactor import install
  36. except ImportError:
  37. from twisted.internet.pollreactor import install
  38. elif platform.getType() == 'posix' and not platform.isMacOSX():
  39. from twisted.internet.pollreactor import install
  40. else:
  41. from twisted.internet.selectreactor import install
  42. except ImportError:
  43. from twisted.internet.selectreactor import install
  44. return install
  45. install = _getInstallFunction(platform)