kopia lustrzana https://github.com/micropython/micropython-lib
uasyncio: Switch to builtin uselect.poll() object.
This is a big step towards supporting uasyncio on baremetal builds (and on unix builds without FFI support).pull/65/merge
rodzic
14e945f1a3
commit
5e10ef8adf
|
@ -1,5 +1,5 @@
|
|||
import errno
|
||||
import select
|
||||
import uselect as select
|
||||
import usocket as _socket
|
||||
from uasyncio.core import *
|
||||
|
||||
|
@ -8,34 +8,41 @@ class EpollEventLoop(EventLoop):
|
|||
|
||||
def __init__(self):
|
||||
EventLoop.__init__(self)
|
||||
self.poller = select.epoll(1)
|
||||
self.poller = select.poll()
|
||||
self.objmap = {}
|
||||
|
||||
def add_reader(self, fd, cb, *args):
|
||||
if __debug__:
|
||||
log.debug("add_reader%s", (fd, cb, args))
|
||||
if args:
|
||||
self.poller.register(fd, select.EPOLLIN | select.EPOLLONESHOT, (cb, args))
|
||||
self.poller.register(fd, select.POLLIN)
|
||||
self.objmap[fd] = (cb, args)
|
||||
else:
|
||||
self.poller.register(fd, select.EPOLLIN | select.EPOLLONESHOT, cb)
|
||||
self.poller.register(fd, select.POLLIN)
|
||||
self.objmap[fd] = cb
|
||||
|
||||
def remove_reader(self, fd):
|
||||
if __debug__:
|
||||
log.debug("remove_reader(%s)", fd)
|
||||
self.poller.unregister(fd)
|
||||
del self.objmap[fd]
|
||||
|
||||
def add_writer(self, fd, cb, *args):
|
||||
if __debug__:
|
||||
log.debug("add_writer%s", (fd, cb, args))
|
||||
if args:
|
||||
self.poller.register(fd, select.EPOLLOUT | select.EPOLLONESHOT, (cb, args))
|
||||
self.poller.register(fd, select.POLLOUT)
|
||||
self.objmap[fd] = (cb, args)
|
||||
else:
|
||||
self.poller.register(fd, select.EPOLLOUT | select.EPOLLONESHOT, cb)
|
||||
self.poller.register(fd, select.POLLOUT)
|
||||
self.objmap[fd] = cb
|
||||
|
||||
def remove_writer(self, fd):
|
||||
if __debug__:
|
||||
log.debug("remove_writer(%s)", fd)
|
||||
try:
|
||||
self.poller.unregister(fd)
|
||||
self.objmap.pop(fd, None)
|
||||
except OSError as e:
|
||||
# StreamWriter.awrite() first tries to write to an fd,
|
||||
# and if that succeeds, yield IOWrite may never be called
|
||||
|
@ -47,12 +54,14 @@ class EpollEventLoop(EventLoop):
|
|||
def wait(self, delay):
|
||||
if __debug__:
|
||||
log.debug("epoll.wait(%d)", delay)
|
||||
# We need one-shot behavior (second arg of 1 to .poll())
|
||||
if delay == -1:
|
||||
res = self.poller.poll(-1)
|
||||
res = self.poller.poll(-1, 1)
|
||||
else:
|
||||
res = self.poller.poll(int(delay * 1000))
|
||||
res = self.poller.poll(int(delay * 1000), 1)
|
||||
#log.debug("epoll result: %s", res)
|
||||
for cb, ev in res:
|
||||
for fd, ev in res:
|
||||
cb = self.objmap[fd]
|
||||
if __debug__:
|
||||
log.debug("Calling IO callback: %r", cb)
|
||||
if isinstance(cb, tuple):
|
||||
|
|
Ładowanie…
Reference in New Issue