commit: 81baf80258393938152d6c8fc53d33d5f85de23c Author: Zac Medico <zmedico <AT> gentoo <DOT> org> AuthorDate: Sun Feb 25 06:17:40 2018 +0000 Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> CommitDate: Sun Feb 25 06:19:18 2018 +0000 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=81baf802
EventLoop: implement call_later for asyncio compat (bug 591760) Bug: https://bugs.gentoo.org/591760 pym/portage/tests/ebuild/test_ipc_daemon.py | 12 ++++++------ pym/portage/util/_eventloop/EventLoop.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/pym/portage/tests/ebuild/test_ipc_daemon.py b/pym/portage/tests/ebuild/test_ipc_daemon.py index fc7916541..1152f31b4 100644 --- a/pym/portage/tests/ebuild/test_ipc_daemon.py +++ b/pym/portage/tests/ebuild/test_ipc_daemon.py @@ -31,7 +31,7 @@ class SleepProcess(ForkProcess): class IpcDaemonTestCase(TestCase): - _SCHEDULE_TIMEOUT = 40000 # 40 seconds + _SCHEDULE_TIMEOUT = 40 # seconds def testIpcDaemon(self): event_loop = global_event_loop() @@ -103,8 +103,8 @@ class IpcDaemonTestCase(TestCase): # Intentionally short timeout test for EventLoop/AsyncScheduler. # Use a ridiculously long sleep_time_s in case the user's # system is heavily loaded (see bug #436334). - sleep_time_s = 600 #600.000 seconds - short_timeout_ms = 10 # 0.010 seconds + sleep_time_s = 600 # seconds + short_timeout_s = 0.010 # seconds for i in range(3): exit_command = ExitCommand() @@ -123,7 +123,7 @@ class IpcDaemonTestCase(TestCase): exit_command.reply_hook = exit_command_callback start_time = time.time() - self._run(event_loop, task_scheduler, short_timeout_ms) + self._run(event_loop, task_scheduler, short_timeout_s) hardlock_cleanup(env['PORTAGE_BUILDDIR'], remove_all_locks=True) @@ -150,7 +150,7 @@ class IpcDaemonTestCase(TestCase): def _run(self, event_loop, task_scheduler, timeout): self._run_done = event_loop.create_future() - timeout_id = event_loop.timeout_add(timeout, + timeout_handle = event_loop.call_later(timeout, self._timeout_callback, task_scheduler) task_scheduler.addExitListener(self._exit_callback) @@ -159,4 +159,4 @@ class IpcDaemonTestCase(TestCase): event_loop.run_until_complete(self._run_done) task_scheduler.wait() finally: - event_loop.source_remove(timeout_id) + timeout_handle.cancel() diff --git a/pym/portage/util/_eventloop/EventLoop.py b/pym/portage/util/_eventloop/EventLoop.py index cd154005f..89ac2a3b3 100644 --- a/pym/portage/util/_eventloop/EventLoop.py +++ b/pym/portage/util/_eventloop/EventLoop.py @@ -684,6 +684,34 @@ class EventLoop(object): # The call_soon method inherits thread safety from the idle_add method. call_soon_threadsafe = call_soon + def call_later(self, delay, callback, *args): + """ + Arrange for the callback to be called after the given delay seconds + (either an int or float). + + An instance of asyncio.Handle is returned, which can be used to cancel + the callback. + + callback will be called exactly once per call to call_later(). If two + callbacks are scheduled for exactly the same time, it is undefined + which will be called first. + + The optional positional args will be passed to the callback when + it is called. If you want the callback to be called with some named + arguments, use a closure or functools.partial(). + + Use functools.partial to pass keywords to the callback. + + @type delay: int or float + @param delay: delay seconds + @type callback: callable + @param callback: a function to call + @return: a handle which can be used to cancel the callback + @rtype: asyncio.Handle (or compatible) + """ + return self._handle(self.timeout_add( + delay * 1000, self._call_soon_callback(callback, args)), self) + _can_poll_device = None
