kopia lustrzana https://github.com/micropython/micropython-lib
asyncio: Implement subclass implementing filedes watching interface.
rodzic
80cea91b98
commit
b42f8e383b
|
@ -6,10 +6,6 @@ def coroutine(f):
|
||||||
return f
|
return f
|
||||||
|
|
||||||
|
|
||||||
def get_event_loop():
|
|
||||||
return EventLoop()
|
|
||||||
|
|
||||||
|
|
||||||
class EventLoop:
|
class EventLoop:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -38,6 +34,10 @@ class EventLoop:
|
||||||
# c = self.q.pop(0)
|
# c = self.q.pop(0)
|
||||||
# c[0](*c[1])
|
# c[0](*c[1])
|
||||||
|
|
||||||
|
def wait(self, delay):
|
||||||
|
# print("Sleeping for:", delay)
|
||||||
|
time.sleep(delay)
|
||||||
|
|
||||||
def run_forever(self):
|
def run_forever(self):
|
||||||
while self.q:
|
while self.q:
|
||||||
# t, cnt, cb, args = self.q.pop(0)
|
# t, cnt, cb, args = self.q.pop(0)
|
||||||
|
@ -45,8 +45,7 @@ class EventLoop:
|
||||||
tnow = self.time()
|
tnow = self.time()
|
||||||
delay = t - tnow
|
delay = t - tnow
|
||||||
if delay > 0:
|
if delay > 0:
|
||||||
# print("Sleeping for:", delay)
|
self.wait(delay)
|
||||||
time.sleep(delay)
|
|
||||||
delay = 0
|
delay = 0
|
||||||
try:
|
try:
|
||||||
ret = next(cb)
|
ret = next(cb)
|
||||||
|
@ -74,6 +73,26 @@ class EventLoop:
|
||||||
def close(self):
|
def close(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
import select
|
||||||
|
|
||||||
|
class EpollEventLoop(EventLoop):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
EventLoop.__init__(self)
|
||||||
|
self.poller = select.epoll(1)
|
||||||
|
|
||||||
|
def add_reader(self, fd, cb, *args):
|
||||||
|
self.poller.register(fd, select.EPOLLIN, (cb, args))
|
||||||
|
|
||||||
|
def add_writer(self, fd, cb, *args):
|
||||||
|
self.poller.register(fd, select.EPOLLOUT, (cb, args))
|
||||||
|
|
||||||
|
def wait(self, delay):
|
||||||
|
res = self.poller.poll(int(delay * 1000))
|
||||||
|
print("poll: ", res)
|
||||||
|
for cb, ev in res:
|
||||||
|
cb[0](*cb[1])
|
||||||
|
|
||||||
|
|
||||||
class SysCall:
|
class SysCall:
|
||||||
|
|
||||||
|
@ -87,6 +106,9 @@ class Sleep(SysCall):
|
||||||
time.sleep(self.args[0])
|
time.sleep(self.args[0])
|
||||||
|
|
||||||
|
|
||||||
|
def get_event_loop():
|
||||||
|
return EpollEventLoop()
|
||||||
|
|
||||||
def sleep(secs):
|
def sleep(secs):
|
||||||
yield Sleep("sleep", secs)
|
yield Sleep("sleep", secs)
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue