initial commit

This commit is contained in:
Victor Stinner 2014-11-07 15:16:57 +01:00
commit a5e78cca81
3 changed files with 73 additions and 0 deletions

17
.hgignore Normal file
View File

@ -0,0 +1,17 @@
.*\.py[co]$
.*~$
.*\.orig$
.*\#.*$
.*@.*$
\.coverage$
htmlcov$
\.DS_Store$
venv$
distribute_setup.py$
distribute-\d+.\d+.\d+.tar.gz$
build$
dist$
.*\.egg-info$
# Directory created by the "tox" command (ex: tox -e py27)
.tox

13
README Normal file
View File

@ -0,0 +1,13 @@
asyncio event loop scheduling callbacks in eventlet.
Implemented:
* call_soon
* call_later
* call_at
To do:
* stop
* run_until_complete
* run_forever

43
aiogreen/__init__.py Normal file
View File

@ -0,0 +1,43 @@
import trollius
import eventlet
class EventLoopPolicy(trollius.DefaultEventLoopPolicy):
pass
class EventLoop(trollius.SelectorEventLoop):
def __init__(self, selector=None):
super(EventLoop, self).__init__(selector=selector)
self._pool = eventlet.GreenPool()
self._timers = []
def _call(self, handle):
if handle._cancelled:
return
handle._run()
def call_soon(self, callback, *args):
handle = trollius.Handle(callback, args, self)
self._pool.spawn(self._call, handle)
return handle
def _call_later(self, handle):
greenthread = eventlet.getcurrent()
self._timers.remove(greenthread)
self._call(handle)
def call_later(self, delay, callback, *args):
# FIXME: cancelling the handle should unschedule the timer
handle = trollius.Handle(callback, args, self)
greenthread = eventlet.spawn_after(delay, self._call_later, handle)
self._timers.append(greenthread)
return handle
def call_at(self, when, callback, *args):
delay = when - self.time()
return self.call_later(delay, callback, *args)
def run_forever(self):
self._pool.waitall()
# FIXME: more efficient code :-)
while self._timers:
eventlet.sleep(0.1)