Message390635
Thanks so much for all the work on os.eventfd(), it's exciting to see it come to fruition.
An eventfd variant of Event (compatible with the threading & multiprocessing APIs) is now as simple as:
class Event:
_ONE = (1).to_bytes(8, byteorder=sys.byteorder)
def __init__(self):
self._event_fd = os.eventfd(0, os.EFD_NONBLOCK)
self._selector = selectors.DefaultSelector()
self._selector.register(self._event_fd, selectors.EVENT_READ)
def is_set(self):
return self.wait(timeout=0)
def set(self):
try:
os.write(self._event_fd, self._ONE)
except BlockingIOError:
pass
def clear(self):
try:
os.read(self._event_fd, 8)
except BlockingIOError:
pass
def wait(self, timeout=None):
return bool(self._selector.select(timeout=timeout))
def fileno(self):
return self._event_fd
Given this now has a fileno() method, it is now possible to wait for the event as part of a broader selector, among other events (e.g. a file or socket becoming available to read or write).
I don't know where (or how) such a variant would fit into stdlib. It's simpler and more lightweight (less fds) than both threading's and multiprocessing's, and could be used from both threads and processes. I'd love some guidance here. (If a maintainer or anyone else reading this wants to use the above code in a PR, feel free -- no copyright claimed or expected for this trivial piece of code above) |
|
Date |
User |
Action |
Args |
2021-04-09 14:57:01 | paravoid | set | recipients:
+ paravoid, christian.heimes, FFY00 |
2021-04-09 14:57:01 | paravoid | set | messageid: <1617980221.33.0.0208013127535.issue40485@roundup.psfhosted.org> |
2021-04-09 14:57:01 | paravoid | link | issue40485 messages |
2021-04-09 14:57:01 | paravoid | create | |
|