aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2020-02-17 10:32:06 -0800
committerZac Medico <zmedico@gentoo.org>2020-02-17 14:29:09 -0800
commitd66e9ec0b10522528d62e18b83e012c1ec121787 (patch)
treecfeef6845f701c23c44fabc773a8e884cc477568 /lib/_emerge
parentfetch: remove secpass import (bug 601252) (diff)
downloadportage-d66e9ec0b10522528d62e18b83e012c1ec121787.tar.gz
portage-d66e9ec0b10522528d62e18b83e012c1ec121787.tar.bz2
portage-d66e9ec0b10522528d62e18b83e012c1ec121787.zip
AsynchronousTask: add coroutine async_start method
Add coroutine async_start coroutine method which calls an _async_start template method. Eventually, subclasses having _start implementations that need to write to a build log will be required to implement an _async_start coroutine method to replace the _start method as discussed in bug 709746. Bug: https://bugs.gentoo.org/709746 Signed-off-by: Zac Medico <zmedico@gentoo.org>
Diffstat (limited to 'lib/_emerge')
-rw-r--r--lib/_emerge/AsynchronousTask.py22
-rw-r--r--lib/_emerge/CompositeTask.py17
-rw-r--r--lib/_emerge/TaskSequence.py7
3 files changed, 37 insertions, 9 deletions
diff --git a/lib/_emerge/AsynchronousTask.py b/lib/_emerge/AsynchronousTask.py
index cf6e6dc44..280ed16da 100644
--- a/lib/_emerge/AsynchronousTask.py
+++ b/lib/_emerge/AsynchronousTask.py
@@ -1,10 +1,11 @@
-# Copyright 1999-2018 Gentoo Foundation
+# Copyright 1999-2020 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
import signal
from portage import os
from portage.util.futures import asyncio
+from portage.util.futures.compat_coroutine import coroutine, coroutine_return
from portage.util.SlotObject import SlotObject
class AsynchronousTask(SlotObject):
@@ -22,6 +23,17 @@ class AsynchronousTask(SlotObject):
_cancelled_returncode = - signal.SIGINT
+ @coroutine
+ def async_start(self):
+ self._start_hook()
+ yield self._async_start()
+
+ @coroutine
+ def _async_start(self):
+ self._start()
+ coroutine_return()
+ yield None
+
def start(self):
"""
Start an asynchronous task and then return as soon as possible.
@@ -29,6 +41,10 @@ class AsynchronousTask(SlotObject):
self._start_hook()
self._start()
+ def _start(self):
+ self.returncode = os.EX_OK
+ self._async_wait()
+
def async_wait(self):
"""
Wait for returncode asynchronously. Notification is available
@@ -49,10 +65,6 @@ class AsynchronousTask(SlotObject):
self._async_wait()
return waiter
- def _start(self):
- self.returncode = os.EX_OK
- self._async_wait()
-
def isAlive(self):
return self.returncode is None
diff --git a/lib/_emerge/CompositeTask.py b/lib/_emerge/CompositeTask.py
index 1edec4a17..ce7136c3d 100644
--- a/lib/_emerge/CompositeTask.py
+++ b/lib/_emerge/CompositeTask.py
@@ -1,8 +1,10 @@
-# Copyright 1999-2018 Gentoo Foundation
+# Copyright 1999-2020 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
from _emerge.AsynchronousTask import AsynchronousTask
from portage import os
+from portage.util.futures import asyncio
+
class CompositeTask(AsynchronousTask):
@@ -97,7 +99,7 @@ class CompositeTask(AsynchronousTask):
def _start_task(self, task, exit_handler):
"""
Register exit handler for the given task, set it
- as self._current_task, and call task.start().
+ as self._current_task, and call task.async_start().
Subclasses can use this as a generic way to start
a task.
@@ -109,7 +111,16 @@ class CompositeTask(AsynchronousTask):
pass
task.addExitListener(exit_handler)
self._current_task = task
- task.start()
+ result = asyncio.ensure_future(task.async_start(), loop=self.scheduler)
+ result.add_done_callback(self._current_task_start_cb)
+
+ def _current_task_start_cb(self, future):
+ try:
+ future.result()
+ except asyncio.CancelledError:
+ self.cancelled = True
+ self._was_cancelled()
+ self._async_wait()
def _task_queued(self, task):
task.addStartListener(self._task_queued_start_handler)
diff --git a/lib/_emerge/TaskSequence.py b/lib/_emerge/TaskSequence.py
index 1f2ba94c2..59ee5686d 100644
--- a/lib/_emerge/TaskSequence.py
+++ b/lib/_emerge/TaskSequence.py
@@ -1,4 +1,4 @@
-# Copyright 1999-2014 Gentoo Foundation
+# Copyright 1999-2020 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
import sys
@@ -42,6 +42,11 @@ class TaskSequence(CompositeTask):
self._start_task(task, self._task_exit_handler)
+ def _current_task_start_cb(self, future):
+ CompositeTask._current_task_start_cb(self, future)
+ if self.cancelled:
+ self._task_queue.clear()
+
def _task_exit_handler(self, task):
if self._default_exit(task) != os.EX_OK:
self.wait()