Intended primarily for use by test frameworks in between tests.
"""
+ old = IOLoop._current.instance
+ if old is not None:
+ old._clear_current_hook()
IOLoop._current.instance = None
+ def _clear_current_hook(self):
+ """Instance method called when an IOLoop ceases to be current.
+
+ May be overridden by subclasses as a counterpart to make_current.
+ """
+ pass
+
@classmethod
def configurable_base(cls):
return IOLoop
class BaseAsyncIOLoop(IOLoop):
def initialize(self, asyncio_loop, close_loop=False, **kwargs):
- super(BaseAsyncIOLoop, self).initialize(**kwargs)
self.asyncio_loop = asyncio_loop
self.close_loop = close_loop
# Maps fd to (fileobj, handler function) pair (as in IOLoop.add_handler)
self.readers = set()
self.writers = set()
self.closing = False
+ super(BaseAsyncIOLoop, self).initialize(**kwargs)
def close(self, all_fds=False):
self.closing = True
def start(self):
old_current = IOLoop.current(instance=False)
- try:
- old_asyncio = asyncio.get_event_loop()
- except RuntimeError:
- old_asyncio = None
try:
self._setup_logging()
self.make_current()
- # This is automatic in 3.5, but must be done manually in 3.4.
- asyncio.set_event_loop(self.asyncio_loop)
self.asyncio_loop.run_forever()
finally:
if old_current is None:
IOLoop.clear_current()
else:
old_current.make_current()
- asyncio.set_event_loop(old_asyncio)
def stop(self):
self.asyncio_loop.stop()
loop.close()
raise
+ def make_current(self):
+ super(AsyncIOLoop, self).make_current()
+ try:
+ self.old_asyncio = asyncio.get_event_loop()
+ except RuntimeError:
+ self.old_asyncio = None
+ asyncio.set_event_loop(self.asyncio_loop)
+
+ def _clear_current_hook(self):
+ asyncio.set_event_loop(self.old_asyncio)
+
def to_tornado_future(asyncio_future):
"""Convert an `asyncio.Future` to a `tornado.concurrent.Future`.
class AsyncIOLoopTest(AsyncTestCase):
def get_new_ioloop(self):
io_loop = AsyncIOLoop()
- asyncio.set_event_loop(io_loop.asyncio_loop)
return io_loop
def test_asyncio_callback(self):
@skipIfNoTwisted
class ConvertDeferredTest(unittest.TestCase):
- def setUp(self):
- if asyncio is not None:
- asyncio.set_event_loop(asyncio.new_event_loop())
-
- def tearDown(self):
- if asyncio is not None:
- asyncio.set_event_loop(None)
-
def test_success(self):
@inlineCallbacks
def fn():
super(AsyncTestCase, self).setUp()
self.io_loop = self.get_new_ioloop()
self.io_loop.make_current()
- if hasattr(self.io_loop, 'asyncio_loop'):
- # Ensure that asyncio's current event loop matches ours.
- asyncio.set_event_loop(self.io_loop.asyncio_loop)
def tearDown(self):
# Clean up Subprocess, so it can be used again with a new ioloop.
# set FD_CLOEXEC on its file descriptors)
self.io_loop.close(all_fds=True)
super(AsyncTestCase, self).tearDown()
- if hasattr(self.io_loop, 'asyncio_loop'):
- asyncio.set_event_loop(None)
# In case an exception escaped or the StackContext caught an exception
# when there wasn't a wait() to re-raise it, do so here.
# This is our last chance to raise an exception in a way that the