1 |
commit: 81baf80258393938152d6c8fc53d33d5f85de23c |
2 |
Author: Zac Medico <zmedico <AT> gentoo <DOT> org> |
3 |
AuthorDate: Sun Feb 25 06:17:40 2018 +0000 |
4 |
Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> |
5 |
CommitDate: Sun Feb 25 06:19:18 2018 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=81baf802 |
7 |
|
8 |
EventLoop: implement call_later for asyncio compat (bug 591760) |
9 |
|
10 |
Bug: https://bugs.gentoo.org/591760 |
11 |
|
12 |
pym/portage/tests/ebuild/test_ipc_daemon.py | 12 ++++++------ |
13 |
pym/portage/util/_eventloop/EventLoop.py | 28 ++++++++++++++++++++++++++++ |
14 |
2 files changed, 34 insertions(+), 6 deletions(-) |
15 |
|
16 |
diff --git a/pym/portage/tests/ebuild/test_ipc_daemon.py b/pym/portage/tests/ebuild/test_ipc_daemon.py |
17 |
index fc7916541..1152f31b4 100644 |
18 |
--- a/pym/portage/tests/ebuild/test_ipc_daemon.py |
19 |
+++ b/pym/portage/tests/ebuild/test_ipc_daemon.py |
20 |
@@ -31,7 +31,7 @@ class SleepProcess(ForkProcess): |
21 |
|
22 |
class IpcDaemonTestCase(TestCase): |
23 |
|
24 |
- _SCHEDULE_TIMEOUT = 40000 # 40 seconds |
25 |
+ _SCHEDULE_TIMEOUT = 40 # seconds |
26 |
|
27 |
def testIpcDaemon(self): |
28 |
event_loop = global_event_loop() |
29 |
@@ -103,8 +103,8 @@ class IpcDaemonTestCase(TestCase): |
30 |
# Intentionally short timeout test for EventLoop/AsyncScheduler. |
31 |
# Use a ridiculously long sleep_time_s in case the user's |
32 |
# system is heavily loaded (see bug #436334). |
33 |
- sleep_time_s = 600 #600.000 seconds |
34 |
- short_timeout_ms = 10 # 0.010 seconds |
35 |
+ sleep_time_s = 600 # seconds |
36 |
+ short_timeout_s = 0.010 # seconds |
37 |
|
38 |
for i in range(3): |
39 |
exit_command = ExitCommand() |
40 |
@@ -123,7 +123,7 @@ class IpcDaemonTestCase(TestCase): |
41 |
|
42 |
exit_command.reply_hook = exit_command_callback |
43 |
start_time = time.time() |
44 |
- self._run(event_loop, task_scheduler, short_timeout_ms) |
45 |
+ self._run(event_loop, task_scheduler, short_timeout_s) |
46 |
|
47 |
hardlock_cleanup(env['PORTAGE_BUILDDIR'], |
48 |
remove_all_locks=True) |
49 |
@@ -150,7 +150,7 @@ class IpcDaemonTestCase(TestCase): |
50 |
|
51 |
def _run(self, event_loop, task_scheduler, timeout): |
52 |
self._run_done = event_loop.create_future() |
53 |
- timeout_id = event_loop.timeout_add(timeout, |
54 |
+ timeout_handle = event_loop.call_later(timeout, |
55 |
self._timeout_callback, task_scheduler) |
56 |
task_scheduler.addExitListener(self._exit_callback) |
57 |
|
58 |
@@ -159,4 +159,4 @@ class IpcDaemonTestCase(TestCase): |
59 |
event_loop.run_until_complete(self._run_done) |
60 |
task_scheduler.wait() |
61 |
finally: |
62 |
- event_loop.source_remove(timeout_id) |
63 |
+ timeout_handle.cancel() |
64 |
|
65 |
diff --git a/pym/portage/util/_eventloop/EventLoop.py b/pym/portage/util/_eventloop/EventLoop.py |
66 |
index cd154005f..89ac2a3b3 100644 |
67 |
--- a/pym/portage/util/_eventloop/EventLoop.py |
68 |
+++ b/pym/portage/util/_eventloop/EventLoop.py |
69 |
@@ -684,6 +684,34 @@ class EventLoop(object): |
70 |
# The call_soon method inherits thread safety from the idle_add method. |
71 |
call_soon_threadsafe = call_soon |
72 |
|
73 |
+ def call_later(self, delay, callback, *args): |
74 |
+ """ |
75 |
+ Arrange for the callback to be called after the given delay seconds |
76 |
+ (either an int or float). |
77 |
+ |
78 |
+ An instance of asyncio.Handle is returned, which can be used to cancel |
79 |
+ the callback. |
80 |
+ |
81 |
+ callback will be called exactly once per call to call_later(). If two |
82 |
+ callbacks are scheduled for exactly the same time, it is undefined |
83 |
+ which will be called first. |
84 |
+ |
85 |
+ The optional positional args will be passed to the callback when |
86 |
+ it is called. If you want the callback to be called with some named |
87 |
+ arguments, use a closure or functools.partial(). |
88 |
+ |
89 |
+ Use functools.partial to pass keywords to the callback. |
90 |
+ |
91 |
+ @type delay: int or float |
92 |
+ @param delay: delay seconds |
93 |
+ @type callback: callable |
94 |
+ @param callback: a function to call |
95 |
+ @return: a handle which can be used to cancel the callback |
96 |
+ @rtype: asyncio.Handle (or compatible) |
97 |
+ """ |
98 |
+ return self._handle(self.timeout_add( |
99 |
+ delay * 1000, self._call_soon_callback(callback, args)), self) |
100 |
+ |
101 |
|
102 |
_can_poll_device = None |