aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2018-04-01 20:46:10 -0700
committerZac Medico <zmedico@gentoo.org>2018-04-08 15:04:37 -0700
commit24f861173ebe747a470deb8489887c067cd46b0f (patch)
treed476da37a5ed2881317d1ef2156389e1da3abcdf /pym/portage/util/_eventloop/EventLoop.py
parentEventLoop: add is_closed method for asyncio compat (bug 591760) (diff)
downloadportage-24f861173ebe747a470deb8489887c067cd46b0f.tar.gz
portage-24f861173ebe747a470deb8489887c067cd46b0f.tar.bz2
portage-24f861173ebe747a470deb8489887c067cd46b0f.zip
EventLoop: implement add/remove_reader/writer for asyncio compat (bug 649588)
Bug: https://bugs.gentoo.org/649588
Diffstat (limited to 'pym/portage/util/_eventloop/EventLoop.py')
-rw-r--r--pym/portage/util/_eventloop/EventLoop.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/pym/portage/util/_eventloop/EventLoop.py b/pym/portage/util/_eventloop/EventLoop.py
index 1bf606354..00568c997 100644
--- a/pym/portage/util/_eventloop/EventLoop.py
+++ b/pym/portage/util/_eventloop/EventLoop.py
@@ -93,6 +93,21 @@ class EventLoop(object):
self._callback(*self._args)
return False
+ class _repeat_callback(object):
+ """
+ Wraps an callback, and always returns True, for callbacks that
+ are supposed to run repeatedly.
+ """
+ __slots__ = ("_args", "_callback")
+
+ def __init__(self, callback, args):
+ self._callback = callback
+ self._args = args
+
+ def __call__(self, fd, event):
+ self._callback(*self._args)
+ return True
+
def __init__(self, main=True):
"""
@param main: If True then this is a singleton instance for use
@@ -569,6 +584,42 @@ class EventLoop(object):
return bool(calls)
+ def add_reader(self, fd, callback, *args):
+ """
+ Start watching the file descriptor for read availability and then
+ call the callback with specified arguments.
+
+ Use functools.partial to pass keywords to the callback.
+ """
+ self.io_add_watch(fd, self.IO_IN, self._repeat_callback(callback, args))
+
+ def remove_reader(self, fd):
+ """
+ Stop watching the file descriptor for read availability.
+ """
+ handler = self._poll_event_handlers.get(fd)
+ if fd is not None:
+ return self.source_remove(handler.source_id)
+ return False
+
+ def add_writer(self, fd, callback, *args):
+ """
+ Start watching the file descriptor for write availability and then
+ call the callback with specified arguments.
+
+ Use functools.partial to pass keywords to the callback.
+ """
+ self.io_add_watch(fd, self.IO_OUT, self._repeat_callback(callback, args))
+
+ def remove_writer(self, fd):
+ """
+ Stop watching the file descriptor for write availability.
+ """
+ handler = self._poll_event_handlers.get(fd)
+ if fd is not None:
+ return self.source_remove(handler.source_id)
+ return False
+
def io_add_watch(self, f, condition, callback, *args):
"""
Like glib.io_add_watch(), your function should return False to