libp2p.tools.async_service package
Submodules
libp2p.tools.async_service.abc module
- class libp2p.tools.async_service.abc.InternalManagerAPI
Bases:
ManagerAPIDefines the API that the Service.manager property exposes.
The InternalManagerAPI / ManagerAPI distinction is in place to ensure that external callers to a service do not try to use the task scheduling functionality as it is only designed to be used internally.
- abstract run_child_service(service: ServiceAPI, daemon: bool = False, name: str | None = None) ManagerAPI
Run a service in the background. If the function throws an exception it will trigger the parent service to be cancelled and be propogated.
If daemon == True then the the service is expected to run indefinitely and will trigger cancellation if the service finishes.
- abstract run_daemon_child_service(service: ServiceAPI, name: str | None = None) ManagerAPI
Run a daemon service in the background.
Equivalent to run_child_service(…, daemon=True).
- abstract run_daemon_task(async_fn: Callable[[...], Awaitable[Any]], *args: Any, name: str | None = None) None
Run a daemon task in the background.
Equivalent to run_task(…, daemon=True).
- abstract run_task(async_fn: Callable[[...], Awaitable[Any]], *args: Any, daemon: bool = False, name: str | None = None) None
Run a task in the background. If the function throws an exception it will trigger the service to be cancelled and be propogated.
If daemon == True then the the task is expected to run indefinitely and will trigger cancellation if the task finishes.
- class libp2p.tools.async_service.abc.ManagerAPI
Bases:
ABC- abstract property did_error: bool
Return boolean indicating if the underlying service threw an exception.
- abstract property is_cancelled: bool
Return boolean indicating if the underlying service has been cancelled.
This can occure externally via the cancel() method or internally due to a task crash or a crash of the actual
ServiceAPI.run()method.
- abstract property is_finished: bool
Return boolean indicating if the underlying service is stopped.
A stopped service will have completed all of the background tasks.
- abstract property is_running: bool
Return boolean indicating if the underlying service is actively running.
A service is considered running if it has been started and has not yet been stopped.
- abstract property is_started: bool
Return boolean indicating if the underlying service has been started.
- abstract async classmethod run_service(service: ServiceAPI) None
Run a service
- class libp2p.tools.async_service.abc.ServiceAPI
Bases:
ABC- abstract get_manager() ManagerAPI
External retrieval of the manager for this service.
Will raise a
LifecycleErrorif the service does not yet have a manager assigned to it.
- class libp2p.tools.async_service.abc.TaskAPI
Bases:
Hashable- parent: TaskWithChildrenAPI | None
libp2p.tools.async_service.base module
- class libp2p.tools.async_service.base.BaseChildServiceTask(name: str, daemon: bool, parent: TaskWithChildrenAPI | None)
Bases:
BaseTask- child_manager: ManagerAPI
- class libp2p.tools.async_service.base.BaseFunctionTask(name: str, daemon: bool, parent: TaskWithChildrenAPI | None, async_fn: Callable[[...], Awaitable[Any]], async_fn_args: Sequence[Any])
Bases:
BaseTaskWithChildren- classmethod iterate_tasks(*tasks: TaskAPI) Iterable[BaseFunctionTask]
Iterate over all tasks of this class type and their children recursively.
- class libp2p.tools.async_service.base.BaseManager(service: ServiceAPI)
Bases:
InternalManagerAPI- property is_running: bool
Return boolean indicating if the underlying service is actively running.
A service is considered running if it has been started and has not yet been stopped.
- run_daemon_child_service(service: ServiceAPI, name: str | None = None) ManagerAPI
Run a daemon service in the background.
Equivalent to run_child_service(…, daemon=True).
- class libp2p.tools.async_service.base.BaseTask(name: str, daemon: bool, parent: TaskWithChildrenAPI | None)
Bases:
TaskAPI
- class libp2p.tools.async_service.base.BaseTaskWithChildren(name: str, daemon: bool, parent: TaskWithChildrenAPI | None)
Bases:
BaseTask,TaskWithChildrenAPI
- class libp2p.tools.async_service.base.Service
Bases:
ServiceAPI- get_manager() ManagerAPI
External retrieval of the manager for this service.
Will raise a
LifecycleErrorif the service does not yet have a manager assigned to it.
- property manager: InternalManagerAPI
Expose the manager as a property here intead of
async_service.abc.ServiceAPIto ensure that anyone using proper type hints will not have access to this property since it isn’t part of that API, while still allowing all subclasses of theasync_service.base.Serviceto access this property directly.
libp2p.tools.async_service.exceptions module
- exception libp2p.tools.async_service.exceptions.DaemonTaskExit
Bases:
ServiceExceptionRaised when an action would violate the service lifecycle rules.
- exception libp2p.tools.async_service.exceptions.LifecycleError
Bases:
ServiceExceptionRaised when an action would violate the service lifecycle rules.
- exception libp2p.tools.async_service.exceptions.ServiceException
Bases:
ExceptionBase class for Service exceptions
- exception libp2p.tools.async_service.exceptions.TooManyChildrenException
Bases:
ServiceExceptionRaised when a service adds too many children. It is a sign of task leakage that needs to be prevented.
libp2p.tools.async_service.stats module
- class libp2p.tools.async_service.stats.Stats(tasks)
Bases:
NamedTuple
libp2p.tools.async_service.trio_service module
- class libp2p.tools.async_service.trio_service.ChildServiceTask(name: str, daemon: bool, parent: TaskWithChildrenAPI | None, child_service: ServiceAPI)
Bases:
BaseChildServiceTask
- class libp2p.tools.async_service.trio_service.FunctionTask(name: str, daemon: bool, parent: TaskWithChildrenAPI | None, async_fn: Callable[[...], Awaitable[Any]], async_fn_args: Sequence[Any])
Bases:
BaseFunctionTask- classmethod iterate_tasks(*tasks: TaskAPI) Iterable[FunctionTask]
Iterate over all FunctionTask instances and their children recursively.
- property trio_task: Task
- class libp2p.tools.async_service.trio_service.TrioManager(service: ServiceAPI)
Bases:
BaseManager- property is_cancelled: bool
Return boolean indicating if the underlying service has been cancelled.
This can occure externally via the cancel() method or internally due to a task crash or a crash of the actual
ServiceAPI.run()method.
- property is_finished: bool
Return boolean indicating if the underlying service is stopped.
A stopped service will have completed all of the background tasks.
- run_child_service(service: ServiceAPI, daemon: bool = False, name: str | None = None) ManagerAPI
Run a service in the background. If the function throws an exception it will trigger the parent service to be cancelled and be propogated.
If daemon == True then the the service is expected to run indefinitely and will trigger cancellation if the service finishes.
- async classmethod run_service(service: ServiceAPI) None
Run a service
- run_task(async_fn: Callable[[...], Awaitable[Any]], *args: Any, daemon: bool = False, name: str | None = None) None
Run a task in the background. If the function throws an exception it will trigger the service to be cancelled and be propogated.
If daemon == True then the the task is expected to run indefinitely and will trigger cancellation if the task finishes.
- libp2p.tools.async_service.trio_service.background_trio_service(service: ServiceAPI) AsyncIterator[ManagerAPI]
Run a service in the background.
The service is running within the context block and will be properly cleaned up upon exiting the context block.
- libp2p.tools.async_service.trio_service.external_api(func: TFunc) TFunc