|
9 | 9 | from types import TracebackType
|
10 | 10 | from typing import Any
|
11 | 11 | from typing import Callable
|
| 12 | +from typing import List |
12 | 13 |
|
| 14 | +import attr |
13 | 15 | import cloudpickle
|
14 | 16 | from pybaum.tree_util import tree_map
|
15 | 17 | from pytask import console
|
@@ -65,6 +67,7 @@ def pytask_execute_build(session: Session) -> bool | None:
|
65 | 67 | with parallel_backend(max_workers=session.config["n_workers"]) as executor:
|
66 | 68 |
|
67 | 69 | session.executor = executor
|
| 70 | + sleeper = _Sleeper() |
68 | 71 |
|
69 | 72 | while session.scheduler.is_active():
|
70 | 73 |
|
@@ -96,6 +99,10 @@ def pytask_execute_build(session: Session) -> bool | None:
|
96 | 99 | running_tasks[task_name] = session.hook.pytask_execute_task(
|
97 | 100 | session=session, task=task
|
98 | 101 | )
|
| 102 | + sleeper.reset() |
| 103 | + |
| 104 | + if not ready_tasks: |
| 105 | + sleeper.increment() |
99 | 106 |
|
100 | 107 | for task_name in list(running_tasks):
|
101 | 108 | future = running_tasks[task_name]
|
@@ -146,7 +153,7 @@ def pytask_execute_build(session: Session) -> bool | None:
|
146 | 153 | if session.should_stop:
|
147 | 154 | break
|
148 | 155 | else:
|
149 |
| - time.sleep(session.config["delay"]) |
| 156 | + sleeper.sleep() |
150 | 157 | except KeyboardInterrupt:
|
151 | 158 | break
|
152 | 159 |
|
@@ -316,3 +323,26 @@ def _create_kwargs_for_task(task: Task) -> dict[Any, Any]:
|
316 | 323 | kwargs[arg_name] = tree_map(lambda x: x.value, attribute)
|
317 | 324 |
|
318 | 325 | return kwargs
|
| 326 | + |
| 327 | + |
| 328 | +@attr.s(kw_only=True) |
| 329 | +class _Sleeper: |
| 330 | + """A sleeper that always sleeps a bit and up to 1 second if you don't wake it up. |
| 331 | +
|
| 332 | + This class controls when the next iteration of the execution loop starts. If new |
| 333 | + tasks are scheduled, the time spent sleeping is reset to a lower value. |
| 334 | +
|
| 335 | + """ |
| 336 | + |
| 337 | + timings = attr.ib(type=List[float], default=[(i / 10) ** 2 for i in range(1, 11)]) |
| 338 | + timing_idx = attr.ib(type=int, default=0) |
| 339 | + |
| 340 | + def reset(self) -> None: |
| 341 | + self.timing_idx = 0 |
| 342 | + |
| 343 | + def increment(self) -> None: |
| 344 | + if self.timing_idx < len(self.timings) - 1: |
| 345 | + self.timing_idx += 1 |
| 346 | + |
| 347 | + def sleep(self) -> None: |
| 348 | + time.sleep(self.timings[self.timing_idx]) |
0 commit comments