From ca9ea0d1b8cc3e506129112c825d36b5ce86ad7b Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 20 Apr 2014 06:05:50 +0300 Subject: [PATCH] asyncio: Support read/write syscalls, and route vals both ways between coros. --- asyncio/asyncio.py | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/asyncio/asyncio.py b/asyncio/asyncio.py index 238565cb..e2e266e7 100644 --- a/asyncio/asyncio.py +++ b/asyncio/asyncio.py @@ -55,12 +55,22 @@ class EventLoop: else: delay = 0 try: - ret = next(cb) -# print("ret:", ret) - if isinstance(ret, Sleep): - delay = ret.args[0] + if args == (): + args = (None,) + print("Send args:", args) + ret = cb.send(*args) + print("ret:", ret) + if isinstance(ret, SysCall): + if isinstance(ret, Sleep): + delay = ret.args[0] + elif isinstance(ret, IORead): + self.add_reader(ret.obj.fileno(), lambda f: self.call_soon(cb, f), ret.obj) + continue + elif isinstance(ret, IOWrite): + self.add_writer(ret.obj.fileno(), lambda f: self.call_soon(cb, f), ret.obj) + continue except StopIteration as e: - print(c, "finished") + print(cb, "finished") continue #self.q.append(c) self.call_later(delay, cb, *args) @@ -116,6 +126,16 @@ class Sleep(SysCall): def handle(self): time.sleep(self.args[0]) +class IORead(SysCall): + + def __init__(self, obj): + self.obj = obj + +class IOWrite(SysCall): + + def __init__(self, obj): + self.obj = obj + def get_event_loop(): return EpollEventLoop()