Skip to content

Commit 78fef25

Browse files
committed
asyncio: Implement subclass implementing filedes watching interface.
1 parent 80ecd3b commit 78fef25

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

asyncio/asyncio.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ def coroutine(f):
66
return f
77

88

9-
def get_event_loop():
10-
return EventLoop()
11-
12-
139
class EventLoop:
1410

1511
def __init__(self):
@@ -38,15 +34,18 @@ def call_at(self, time, callback, *args):
3834
# c = self.q.pop(0)
3935
# c[0](*c[1])
4036

37+
def wait(self, delay):
38+
# print("Sleeping for:", delay)
39+
time.sleep(delay)
40+
4141
def run_forever(self):
4242
while self.q:
4343
# t, cnt, cb, args = self.q.pop(0)
4444
t, cnt, cb, args = heapq.heappop(self.q)
4545
tnow = self.time()
4646
delay = t - tnow
4747
if delay > 0:
48-
# print("Sleeping for:", delay)
49-
time.sleep(delay)
48+
self.wait(delay)
5049
delay = 0
5150
try:
5251
ret = next(cb)
@@ -74,6 +73,26 @@ def run_until_complete(self, coro):
7473
def close(self):
7574
pass
7675

76+
import select
77+
78+
class EpollEventLoop(EventLoop):
79+
80+
def __init__(self):
81+
EventLoop.__init__(self)
82+
self.poller = select.epoll(1)
83+
84+
def add_reader(self, fd, cb, *args):
85+
self.poller.register(fd, select.EPOLLIN, (cb, args))
86+
87+
def add_writer(self, fd, cb, *args):
88+
self.poller.register(fd, select.EPOLLOUT, (cb, args))
89+
90+
def wait(self, delay):
91+
res = self.poller.poll(int(delay * 1000))
92+
print("poll: ", res)
93+
for cb, ev in res:
94+
cb[0](*cb[1])
95+
7796

7897
class SysCall:
7998

@@ -87,6 +106,9 @@ def handle(self):
87106
time.sleep(self.args[0])
88107

89108

109+
def get_event_loop():
110+
return EpollEventLoop()
111+
90112
def sleep(secs):
91113
yield Sleep("sleep", secs)
92114

0 commit comments

Comments
 (0)