Gentoo Archives: gentoo-portage-dev

From: Zac Medico <zmedico@g.o>
To: gentoo-portage-dev@l.g.o
Cc: Zac Medico <zmedico@g.o>
Subject: [gentoo-portage-dev] [PATCH] EventLoop: add run_until_complete method (bug 591760)
Date: Sat, 20 Aug 2016 22:20:46
Message-Id: 1471731503-12479-1-git-send-email-zmedico@gentoo.org
1 This emulates the asyncio.AbstractEventLoop.run_until_complete(future)
2 interface, which will make it possible to reduce latency in situations
3 where it is desirable for a loop to exit at the earliest opportunity.
4
5 X-Gentoo-bug: 591760
6 X-Gentoo-bug-url: https://bugs.gentoo.org/show_bug.cgi?id=591760
7 ---
8 pym/portage/tests/ebuild/test_ipc_daemon.py | 23 ++++++++++++++---------
9 pym/portage/util/_eventloop/EventLoop.py | 17 ++++++++++++++++-
10 2 files changed, 30 insertions(+), 10 deletions(-)
11
12 diff --git a/pym/portage/tests/ebuild/test_ipc_daemon.py b/pym/portage/tests/ebuild/test_ipc_daemon.py
13 index 835f51f..68f139a 100644
14 --- a/pym/portage/tests/ebuild/test_ipc_daemon.py
15 +++ b/pym/portage/tests/ebuild/test_ipc_daemon.py
16 @@ -1,4 +1,4 @@
17 -# Copyright 2010-2015 Gentoo Foundation
18 +# Copyright 2010-2016 Gentoo Foundation
19 # Distributed under the terms of the GNU General Public License v2
20
21 import tempfile
22 @@ -16,6 +16,7 @@ from portage.util import ensure_dirs
23 from portage.util._async.ForkProcess import ForkProcess
24 from portage.util._async.TaskScheduler import TaskScheduler
25 from portage.util._eventloop.global_event_loop import global_event_loop
26 +from portage.util.futures.futures import Future
27 from _emerge.SpawnProcess import SpawnProcess
28 from _emerge.EbuildBuildDir import EbuildBuildDir
29 from _emerge.EbuildIpcDaemon import EbuildIpcDaemon
30 @@ -140,19 +141,23 @@ class IpcDaemonTestCase(TestCase):
31 build_dir.unlock()
32 shutil.rmtree(tmpdir)
33
34 - def _timeout_callback(self):
35 - self._timed_out = True
36 + def _timeout_callback(self, task_scheduler):
37 + task_scheduler.cancel()
38 + self._exit_callback(task_scheduler)
39 +
40 + def _exit_callback(self, task_scheduler):
41 + if not self._run_done.done():
42 + self._run_done.set_result(True)
43
44 def _run(self, event_loop, task_scheduler, timeout):
45 - self._timed_out = False
46 - timeout_id = event_loop.timeout_add(timeout, self._timeout_callback)
47 + self._run_done = Future()
48 + timeout_id = event_loop.timeout_add(timeout,
49 + self._timeout_callback, task_scheduler)
50 + task_scheduler.addExitListener(self._exit_callback)
51
52 try:
53 task_scheduler.start()
54 - while not self._timed_out and task_scheduler.poll() is None:
55 - event_loop.iteration()
56 - if self._timed_out:
57 - task_scheduler.cancel()
58 + event_loop.run_until_complete(self._run_done)
59 task_scheduler.wait()
60 finally:
61 event_loop.source_remove(timeout_id)
62 diff --git a/pym/portage/util/_eventloop/EventLoop.py b/pym/portage/util/_eventloop/EventLoop.py
63 index 8095400..8f13de3 100644
64 --- a/pym/portage/util/_eventloop/EventLoop.py
65 +++ b/pym/portage/util/_eventloop/EventLoop.py
66 @@ -1,4 +1,4 @@
67 -# Copyright 1999-2014 Gentoo Foundation
68 +# Copyright 1999-2016 Gentoo Foundation
69 # Distributed under the terms of the GNU General Public License v2
70
71 from __future__ import division
72 @@ -577,6 +577,21 @@ class EventLoop(object):
73 del self._poll_event_handlers[f]
74 return True
75
76 + def run_until_complete(self, future):
77 + """
78 + Run until the Future is done.
79 +
80 + @type future: asyncio.Future
81 + @param future: a Future to wait for
82 + @rtype: object
83 + @return: the Future's result
84 + @raise: the Future's exception
85 + """
86 + while not future.done():
87 + self.iteration()
88 +
89 + return future.result()
90 +
91 _can_poll_device = None
92
93 def can_poll_device():
94 --
95 2.7.4

Replies