From 5ed6b443dbb49b9189942dcc6bb8c30ef9536034 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 21 Jul 2014 14:30:31 +0200 Subject: [PATCH] Also use loop.create_task() with asyncio --- greenio/__init__.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/greenio/__init__.py b/greenio/__init__.py index 393d10c..49f4e38 100644 --- a/greenio/__init__.py +++ b/greenio/__init__.py @@ -24,28 +24,29 @@ except ImportError: if asyncio is None: asyncio = trollius +def _create_task(coro): + loop = asyncio.get_event_loop() + if hasattr(loop, 'create_task'): + return loop.create_task(coro) + else: + return GreenTask(coro, loop=loop) + + if trollius is not None: def _async(future): # trollius iscoroutine() accepts trollius and asyncio coroutine # objects if trollius.iscoroutine(future): - loop = asyncio.get_event_loop() - return loop.create_task(future) + return _create_task(future) else: return future - - def _create_task(coro): - loop = asyncio.get_event_loop() - return loop.create_task(coro) else: def _async(future): if asyncio.iscoroutine(future): - return GreenTask(future) + return _create_task(future) else: return future - def _create_task(coro): - return GreenTask(coro) _FUTURE_CLASSES = (asyncio.Future,) if trollius is not None and trollius is not asyncio: