Skip to content

taskwait

Wait for a task to complete. This is a very small package and does not really warrant this level of complexity in documentation. We're using this to help us bootstrap up some ideas around python doc generation.

Reference

Wait for a task to complete.

This module is inspired by our R "logwatch" package.

Result dataclass

Result of waiting on a task.

Attributes

status (str): The final status, returned by the status() method start (float): The task start time, in seconds since the Epoch end (float): The task end time, in seconds since the Epoch

Source code in src/taskwait/__init__.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
@dataclass
class Result:
    """
    Result of waiting on a task.

    Attributes
    ----------
      status (str):
        The final status, returned by the ``status()`` method
      start (float):
        The task start time, in seconds since the Epoch
      end (float):
        The task end time, in seconds since the Epoch

    """

    status: str
    start: float
    end: float

Task

Bases: ABC

Base class for tasks.

Inherit from this class to create something suitable to pass into taskwait.

Attributes

status_waiting (set[str]): A set of statuses that are interpreted as "waiting" status_running (set[str]): A set of statuses that are interpreted as "running"

Source code in src/taskwait/__init__.py
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
42
43
44
45
46
class Task(ABC):
    """
    Base class for tasks.

    Inherit from this class to create something suitable to pass into
    taskwait.

    Attributes
    ----------
      status_waiting (set[str]):
          A set of statuses that are interpreted as "waiting"
      status_running (set[str]):
        A set of statuses that are interpreted as "running"

    """

    status_waiting: set[str]
    status_running: set[str]

    @abstractmethod
    def status(self) -> str:
        """Query for the status of the task."""
        pass  # pragma: no cover

    @abstractmethod
    def log(self) -> list[str] | None:
        """Fetch logs for the task, if available."""
        return None  # pragma: no cover

    @abstractmethod
    def has_log(self) -> bool:
        """Indicate if this task **may** produce logs (now or in future)."""
        pass  # pragma: no cover

has_log() abstractmethod

Indicate if this task may produce logs (now or in future).

Source code in src/taskwait/__init__.py
43
44
45
46
@abstractmethod
def has_log(self) -> bool:
    """Indicate if this task **may** produce logs (now or in future)."""
    pass  # pragma: no cover

log() abstractmethod

Fetch logs for the task, if available.

Source code in src/taskwait/__init__.py
38
39
40
41
@abstractmethod
def log(self) -> list[str] | None:
    """Fetch logs for the task, if available."""
    return None  # pragma: no cover

status() abstractmethod

Query for the status of the task.

Source code in src/taskwait/__init__.py
33
34
35
36
@abstractmethod
def status(self) -> str:
    """Query for the status of the task."""
    pass  # pragma: no cover

taskwait(task, *, show_log=True, progress=True, poll=1, timeout=None)

Wait for a task to complete.

Parameters:

Name Type Description Default
task Task

The task to wait on.

required
show_log bool

Show logs, if available, while waiting?

True
progress bool

Show a progress bar while waiting? Only shown while running if show_log is False.

True
poll float

Period to poll for new status/logs, in seconds.

1
timeout float | None

(float | None): Time, in seconds, to wait before throwing a TimeoutError. If None, we wait forever.

None
Source code in src/taskwait/__init__.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
def taskwait(
    task: Task,
    *,
    show_log: bool = True,
    progress: bool = True,
    poll: float = 1,
    timeout: float | None = None,
) -> Result:
    """
    Wait for a task to complete.

    Args:
      task (Task): The task to wait on.
      show_log (bool): Show logs, if available, while waiting?
      progress (bool): Show a progress bar while waiting? Only shown
        while running if `show_log` is `False`.
      poll (float): Period to poll for new status/logs, in seconds.
      timeout: (float | None): Time, in seconds, to wait before
        throwing a `TimeoutError`.  If `None`, we wait forever.

    """
    t = _RunningTask(
        task, show_log=show_log, progress=progress, poll=poll, timeout=timeout
    )
    return t.wait()