Skip to content

Commit 5e10ef8

Browse files
committed
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).
1 parent 14e945f commit 5e10ef8

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

uasyncio/uasyncio/__init__.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import errno
2-
import select
2+
import uselect as select
33
import usocket as _socket
44
from uasyncio.core import *
55

@@ -8,34 +8,41 @@ class EpollEventLoop(EventLoop):
88

99
def __init__(self):
1010
EventLoop.__init__(self)
11-
self.poller = select.epoll(1)
11+
self.poller = select.poll()
12+
self.objmap = {}
1213

1314
def add_reader(self, fd, cb, *args):
1415
if __debug__:
1516
log.debug("add_reader%s", (fd, cb, args))
1617
if args:
17-
self.poller.register(fd, select.EPOLLIN | select.EPOLLONESHOT, (cb, args))
18+
self.poller.register(fd, select.POLLIN)
19+
self.objmap[fd] = (cb, args)
1820
else:
19-
self.poller.register(fd, select.EPOLLIN | select.EPOLLONESHOT, cb)
21+
self.poller.register(fd, select.POLLIN)
22+
self.objmap[fd] = cb
2023

2124
def remove_reader(self, fd):
2225
if __debug__:
2326
log.debug("remove_reader(%s)", fd)
2427
self.poller.unregister(fd)
28+
del self.objmap[fd]
2529

2630
def add_writer(self, fd, cb, *args):
2731
if __debug__:
2832
log.debug("add_writer%s", (fd, cb, args))
2933
if args:
30-
self.poller.register(fd, select.EPOLLOUT | select.EPOLLONESHOT, (cb, args))
34+
self.poller.register(fd, select.POLLOUT)
35+
self.objmap[fd] = (cb, args)
3136
else:
32-
self.poller.register(fd, select.EPOLLOUT | select.EPOLLONESHOT, cb)
37+
self.poller.register(fd, select.POLLOUT)
38+
self.objmap[fd] = cb
3339

3440
def remove_writer(self, fd):
3541
if __debug__:
3642
log.debug("remove_writer(%s)", fd)
3743
try:
3844
self.poller.unregister(fd)
45+
self.objmap.pop(fd, None)
3946
except OSError as e:
4047
# StreamWriter.awrite() first tries to write to an fd,
4148
# and if that succeeds, yield IOWrite may never be called
@@ -47,12 +54,14 @@ def remove_writer(self, fd):
4754
def wait(self, delay):
4855
if __debug__:
4956
log.debug("epoll.wait(%d)", delay)
57+
# We need one-shot behavior (second arg of 1 to .poll())
5058
if delay == -1:
51-
res = self.poller.poll(-1)
59+
res = self.poller.poll(-1, 1)
5260
else:
53-
res = self.poller.poll(int(delay * 1000))
61+
res = self.poller.poll(int(delay * 1000), 1)
5462
#log.debug("epoll result: %s", res)
55-
for cb, ev in res:
63+
for fd, ev in res:
64+
cb = self.objmap[fd]
5665
if __debug__:
5766
log.debug("Calling IO callback: %r", cb)
5867
if isinstance(cb, tuple):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy