@@ -6,10 +6,6 @@ def coroutine(f):
6
6
return f
7
7
8
8
9
- def get_event_loop ():
10
- return EventLoop ()
11
-
12
-
13
9
class EventLoop :
14
10
15
11
def __init__ (self ):
@@ -38,15 +34,18 @@ def call_at(self, time, callback, *args):
38
34
# c = self.q.pop(0)
39
35
# c[0](*c[1])
40
36
37
+ def wait (self , delay ):
38
+ # print("Sleeping for:", delay)
39
+ time .sleep (delay )
40
+
41
41
def run_forever (self ):
42
42
while self .q :
43
43
# t, cnt, cb, args = self.q.pop(0)
44
44
t , cnt , cb , args = heapq .heappop (self .q )
45
45
tnow = self .time ()
46
46
delay = t - tnow
47
47
if delay > 0 :
48
- # print("Sleeping for:", delay)
49
- time .sleep (delay )
48
+ self .wait (delay )
50
49
delay = 0
51
50
try :
52
51
ret = next (cb )
@@ -74,6 +73,26 @@ def run_until_complete(self, coro):
74
73
def close (self ):
75
74
pass
76
75
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
+
77
96
78
97
class SysCall :
79
98
@@ -87,6 +106,9 @@ def handle(self):
87
106
time .sleep (self .args [0 ])
88
107
89
108
109
+ def get_event_loop ():
110
+ return EpollEventLoop ()
111
+
90
112
def sleep (secs ):
91
113
yield Sleep ("sleep" , secs )
92
114
0 commit comments