From 9bb4f6b3b1dd9d4725f8dc1239bc2ea12fb612ec Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 3 Nov 2014 06:44:05 +0200 Subject: [PATCH] uasyncio.core: Implement async() and Task() for CPython compatibility. --- uasyncio.core/uasyncio/core.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/uasyncio.core/uasyncio/core.py b/uasyncio.core/uasyncio/core.py index 024e61fe..1c5632e2 100644 --- a/uasyncio.core/uasyncio/core.py +++ b/uasyncio.core/uasyncio/core.py @@ -132,15 +132,32 @@ class IOWriteDone(SysCall): pass +_event_loop = None +_event_loop_class = EventLoop def get_event_loop(): - return EventLoop() + global _event_loop + if _event_loop is None: + _event_loop = _event_loop_class() + return _event_loop + +def sleep(secs): + yield Sleep(secs) def coroutine(f): return f -def async(coro): - # We don't have Task bloat, so op is null +# +# The functions below are deprecated in uasyncio, and provided only +# for compatibility with CPython asyncio +# + +def async(coro, loop=_event_loop): + _event_loop.call_soon(coro) + # CPython asyncio incompatibility: we don't return Task object return coro -def sleep(secs): - yield Sleep(secs) + +# CPython asyncio incompatibility: Task is a function, not a class (for efficiency) +def Task(coro, loop=_event_loop): + # Same as async() + _event_loop.call_soon(coro)