flowno.core.event_loop.tasks
Task management for the Flowno event loop.
This module provides classes for creating, managing, and interacting with asynchronous tasks in the Flowno event loop. The main class is TaskHandle, which represents a spawned task and allows operations like cancellation and joining.
Examples
>>> from flowno.core.event_loop.event_loop import EventLoop
>>> from flowno.core.event_loop.primitives import spawn
>>>
>>> async def worker():
... # Some async work
... return 42
>>>
>>> async def main():
... # Spawn a new task
... task = await spawn(worker())
...
... # Check if it's a TaskHandle
... print(f"task is a TaskHandle: {isinstance(task, TaskHandle)}")
...
... # Wait for the task to complete
... result = await task.join()
... print(f"Task result: {result}")
...
... return result
>>>
>>> # Run the example
>>> loop = EventLoop()
>>> result = loop.run_until_complete(main(), join=True)
task is a TaskHandle: True
Task result: 42
>>> print(result)
42
- exception flowno.core.event_loop.tasks.TaskCancelled(by: TaskHandle[object])[source]
Exception raised when a task is cancelled.
This exception is raised when attempting to join a task that has been cancelled.
- final class flowno.core.event_loop.tasks.TaskHandle(event_loop: EventLoop, raw_task: Coroutine[Command, object, _T_co])[source]
A handle to a spawned task that allows cancellation and joining.
TaskHandle objects are returned by the spawn primitive and represent concurrent tasks running in the event loop. They can be used to:
Check if a task has completed, failed, or been cancelled
Cancel a running task
Join (wait for) a task to complete and get its return value
- cancel() bool [source]
Cancel this task if it’s still running.
- Returns:
True if the task was successfully cancelled, False if it was already finished or had an error.
- property is_cancelled: bool
Check if the task was cancelled.
- Returns:
True if the task was cancelled.
- property is_error: bool
Check if the task completed with an error.
- Returns:
True if the task raised an exception (not including cancellation).
- property is_finished: bool
Check if the task completed successfully.
- Returns:
True if the task has finished execution without errors.
- join() Generator[JoinCommand[_T_co], object, _T_co] [source]
Wait for this task to complete and get its result.
This is a coroutine that yields a JoinCommand for the event loop to process. When the task completes, this coroutine will resume with its result.
- Returns:
The value returned by the task.
- Raises:
Exception – Any exception that was raised by the task.
TaskCancelled – If the task was cancelled.