aboutsummaryrefslogtreecommitdiff
blob: 4c2f7a5712b08f7a2ccdd91ab60c39de0905562d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Copyright 2018-2023 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

import os
import signal

from _emerge.AsynchronousTask import AsynchronousTask
from portage.util.futures import asyncio


class AsyncTaskFuture(AsynchronousTask):
    """
    Wraps a Future in an AsynchronousTask, which is useful for
    scheduling with TaskScheduler.
    """

    __slots__ = ("future",)

    def _start(self):
        self.future = asyncio.ensure_future(self.future, self.scheduler)
        self.future.add_done_callback(self._done_callback)

    def isAlive(self):
        """
        Returns True if self.future is an asyncio.Future that is not done.
        """
        return isinstance(self.future, asyncio.Future) and not self.future.done()

    def _cancel(self):
        if not self.future.done():
            self.future.cancel()

    def _done_callback(self, future):
        if future.cancelled():
            self.cancelled = True
            self.returncode = -signal.SIGINT
        elif future.exception() is None:
            self.returncode = os.EX_OK
        else:
            self.returncode = 1
        self._async_wait()