1 |
commit: 995f0f983386e2a82dbce65d4366ee7f58f59138 |
2 |
Author: Zac Medico <zmedico <AT> gentoo <DOT> org> |
3 |
AuthorDate: Sat Aug 20 21:59:55 2016 +0000 |
4 |
Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> |
5 |
CommitDate: Tue Aug 23 16:33:47 2016 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=995f0f98 |
7 |
|
8 |
EventLoop: add run_until_complete method (bug 591760) |
9 |
|
10 |
This emulates the asyncio.AbstractEventLoop.run_until_complete(future) |
11 |
interface, which will make it possible to reduce latency in situations |
12 |
where it is desirable for a loop to exit at the earliest opportunity. |
13 |
|
14 |
The most tangible benefit of this change is that it provides a |
15 |
migration path to asyncio, which will allow us to rely on a standard |
16 |
library instead of our own internal event loop implementation. |
17 |
|
18 |
In order to migrate to asyncio, more work is planned: |
19 |
|
20 |
* Migrate all internal use of the EventLoop.iteration method to the new |
21 |
run_until_complete(future) method, and remove the EventLoop.iteration |
22 |
method (or make it private as long as it's needed to implement |
23 |
run_until_complete for older python versions). |
24 |
|
25 |
* Implement all EventLoop methods using asyncio.AbstractEventLoop |
26 |
methods (but keep existing implementations for use with older python). |
27 |
|
28 |
X-Gentoo-bug: 591760 |
29 |
X-Gentoo-bug-url: https://bugs.gentoo.org/show_bug.cgi?id=591760 |
30 |
Acked-by: Alexander Berntsen <bernalex <AT> gentoo.org> |
31 |
|
32 |
pym/portage/tests/ebuild/test_ipc_daemon.py | 23 ++++++++++++++--------- |
33 |
pym/portage/util/_eventloop/EventLoop.py | 17 ++++++++++++++++- |
34 |
2 files changed, 30 insertions(+), 10 deletions(-) |
35 |
|
36 |
diff --git a/pym/portage/tests/ebuild/test_ipc_daemon.py b/pym/portage/tests/ebuild/test_ipc_daemon.py |
37 |
index 835f51f..68f139a 100644 |
38 |
--- a/pym/portage/tests/ebuild/test_ipc_daemon.py |
39 |
+++ b/pym/portage/tests/ebuild/test_ipc_daemon.py |
40 |
@@ -1,4 +1,4 @@ |
41 |
-# Copyright 2010-2015 Gentoo Foundation |
42 |
+# Copyright 2010-2016 Gentoo Foundation |
43 |
# Distributed under the terms of the GNU General Public License v2 |
44 |
|
45 |
import tempfile |
46 |
@@ -16,6 +16,7 @@ from portage.util import ensure_dirs |
47 |
from portage.util._async.ForkProcess import ForkProcess |
48 |
from portage.util._async.TaskScheduler import TaskScheduler |
49 |
from portage.util._eventloop.global_event_loop import global_event_loop |
50 |
+from portage.util.futures.futures import Future |
51 |
from _emerge.SpawnProcess import SpawnProcess |
52 |
from _emerge.EbuildBuildDir import EbuildBuildDir |
53 |
from _emerge.EbuildIpcDaemon import EbuildIpcDaemon |
54 |
@@ -140,19 +141,23 @@ class IpcDaemonTestCase(TestCase): |
55 |
build_dir.unlock() |
56 |
shutil.rmtree(tmpdir) |
57 |
|
58 |
- def _timeout_callback(self): |
59 |
- self._timed_out = True |
60 |
+ def _timeout_callback(self, task_scheduler): |
61 |
+ task_scheduler.cancel() |
62 |
+ self._exit_callback(task_scheduler) |
63 |
+ |
64 |
+ def _exit_callback(self, task_scheduler): |
65 |
+ if not self._run_done.done(): |
66 |
+ self._run_done.set_result(True) |
67 |
|
68 |
def _run(self, event_loop, task_scheduler, timeout): |
69 |
- self._timed_out = False |
70 |
- timeout_id = event_loop.timeout_add(timeout, self._timeout_callback) |
71 |
+ self._run_done = Future() |
72 |
+ timeout_id = event_loop.timeout_add(timeout, |
73 |
+ self._timeout_callback, task_scheduler) |
74 |
+ task_scheduler.addExitListener(self._exit_callback) |
75 |
|
76 |
try: |
77 |
task_scheduler.start() |
78 |
- while not self._timed_out and task_scheduler.poll() is None: |
79 |
- event_loop.iteration() |
80 |
- if self._timed_out: |
81 |
- task_scheduler.cancel() |
82 |
+ event_loop.run_until_complete(self._run_done) |
83 |
task_scheduler.wait() |
84 |
finally: |
85 |
event_loop.source_remove(timeout_id) |
86 |
|
87 |
diff --git a/pym/portage/util/_eventloop/EventLoop.py b/pym/portage/util/_eventloop/EventLoop.py |
88 |
index 8095400..8f13de3 100644 |
89 |
--- a/pym/portage/util/_eventloop/EventLoop.py |
90 |
+++ b/pym/portage/util/_eventloop/EventLoop.py |
91 |
@@ -1,4 +1,4 @@ |
92 |
-# Copyright 1999-2014 Gentoo Foundation |
93 |
+# Copyright 1999-2016 Gentoo Foundation |
94 |
# Distributed under the terms of the GNU General Public License v2 |
95 |
|
96 |
from __future__ import division |
97 |
@@ -577,6 +577,21 @@ class EventLoop(object): |
98 |
del self._poll_event_handlers[f] |
99 |
return True |
100 |
|
101 |
+ def run_until_complete(self, future): |
102 |
+ """ |
103 |
+ Run until the Future is done. |
104 |
+ |
105 |
+ @type future: asyncio.Future |
106 |
+ @param future: a Future to wait for |
107 |
+ @rtype: object |
108 |
+ @return: the Future's result |
109 |
+ @raise: the Future's exception |
110 |
+ """ |
111 |
+ while not future.done(): |
112 |
+ self.iteration() |
113 |
+ |
114 |
+ return future.result() |
115 |
+ |
116 |
_can_poll_device = None |
117 |
|
118 |
def can_poll_device(): |