ioloop.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # coding: utf-8
  2. """tornado IOLoop API with zmq compatibility
  3. This module is deprecated in pyzmq 17.
  4. To use zmq with tornado,
  5. eventloop integration is no longer required
  6. and tornado itself should be used.
  7. """
  8. # Copyright (C) PyZMQ Developers
  9. # Distributed under the terms of the Modified BSD License.
  10. from __future__ import absolute_import, division, with_statement
  11. import time
  12. import warnings
  13. try:
  14. import tornado
  15. from tornado.log import gen_log
  16. from tornado import ioloop
  17. if not hasattr(ioloop.IOLoop, 'configurable_default'):
  18. raise ImportError("Tornado too old: %s" % getattr(tornado, 'version', 'unknown'))
  19. except ImportError:
  20. from .minitornado import ioloop
  21. from .minitornado.log import gen_log
  22. PeriodicCallback = ioloop.PeriodicCallback
  23. class DelayedCallback(PeriodicCallback):
  24. """Schedules the given callback to be called once.
  25. The callback is called once, after callback_time milliseconds.
  26. `start` must be called after the DelayedCallback is created.
  27. The timeout is calculated from when `start` is called.
  28. """
  29. def __init__(self, callback, callback_time, io_loop=None):
  30. # PeriodicCallback require callback_time to be positive
  31. warnings.warn("""DelayedCallback is deprecated.
  32. Use loop.add_timeout instead.""", DeprecationWarning)
  33. callback_time = max(callback_time, 1e-3)
  34. super(DelayedCallback, self).__init__(callback, callback_time, io_loop)
  35. def start(self):
  36. """Starts the timer."""
  37. self._running = True
  38. self._firstrun = True
  39. self._next_timeout = time.time() + self.callback_time / 1000.0
  40. self.io_loop.add_timeout(self._next_timeout, self._run)
  41. def _run(self):
  42. if not self._running: return
  43. self._running = False
  44. try:
  45. self.callback()
  46. except Exception:
  47. gen_log.error("Error in delayed callback", exc_info=True)
  48. def _deprecated():
  49. if _deprecated.called:
  50. return
  51. _deprecated.called = True
  52. warnings.warn("zmq.eventloop.ioloop is deprecated in pyzmq 17."
  53. " pyzmq now works with default tornado and asyncio eventloops.",
  54. DeprecationWarning, stacklevel=3)
  55. _deprecated.called = False
  56. # resolve 'true' default loop
  57. if '.minitornado.' in ioloop.__name__:
  58. from ._deprecated import ZMQIOLoop as _IOLoop
  59. else:
  60. _IOLoop = ioloop.IOLoop
  61. while _IOLoop.configurable_default() is not _IOLoop:
  62. _IOLoop = _IOLoop.configurable_default()
  63. class ZMQIOLoop(_IOLoop):
  64. """DEPRECATED: No longer needed as of pyzmq-17
  65. PyZMQ tornado integration now works with the default :mod:`tornado.ioloop.IOLoop`.
  66. """
  67. def __init__(self, *args, **kwargs):
  68. _deprecated()
  69. # super is object, which takes no args
  70. return super(ZMQIOLoop, self).__init__()
  71. @classmethod
  72. def instance(cls, *args, **kwargs):
  73. """Returns a global `IOLoop` instance.
  74. Most applications have a single, global `IOLoop` running on the
  75. main thread. Use this method to get this instance from
  76. another thread. To get the current thread's `IOLoop`, use `current()`.
  77. """
  78. # install ZMQIOLoop as the active IOLoop implementation
  79. # when using tornado 3
  80. ioloop.IOLoop.configure(cls)
  81. _deprecated()
  82. loop = ioloop.IOLoop.instance(*args, **kwargs)
  83. return loop
  84. @classmethod
  85. def current(cls, *args, **kwargs):
  86. """Returns the current thread’s IOLoop.
  87. """
  88. # install ZMQIOLoop as the active IOLoop implementation
  89. # when using tornado 3
  90. ioloop.IOLoop.configure(cls)
  91. _deprecated()
  92. loop = ioloop.IOLoop.current(*args, **kwargs)
  93. return loop
  94. # public API name
  95. IOLoop = ZMQIOLoop
  96. def install():
  97. """DEPRECATED
  98. pyzmq 17 no longer needs any special integration for tornado.
  99. """
  100. _deprecated()
  101. ioloop.IOLoop.configure(ZMQIOLoop)
  102. # if minitornado is used, fallback on deprecated ZMQIOLoop, install implementations
  103. if '.minitornado.' in ioloop.__name__:
  104. from ._deprecated import ZMQIOLoop, install, IOLoop