Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions docs/libp2p.tools.anyio_service.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
libp2p.tools.anyio_service package
===================================

Submodules
----------

libp2p.tools.anyio_service.abc module
--------------------------------------

.. automodule:: libp2p.tools.anyio_service.abc
:members:
:undoc-members:
:show-inheritance:

libp2p.tools.anyio_service.base module
---------------------------------------

.. automodule:: libp2p.tools.anyio_service.base
:members:
:undoc-members:
:show-inheritance:

libp2p.tools.anyio_service.exceptions module
---------------------------------------------

.. automodule:: libp2p.tools.anyio_service.exceptions
:members:
:undoc-members:
:show-inheritance:

libp2p.tools.anyio_service.stats module
----------------------------------------

.. automodule:: libp2p.tools.anyio_service.stats
:members:
:undoc-members:
:show-inheritance:

libp2p.tools.anyio_service.typing module
-----------------------------------------

.. automodule:: libp2p.tools.anyio_service.typing
:members:
:undoc-members:
:show-inheritance:

Module contents
---------------

.. automodule:: libp2p.tools.anyio_service
:members:
:undoc-members:
:show-inheritance:
61 changes: 0 additions & 61 deletions docs/libp2p.tools.async_service.rst

This file was deleted.

2 changes: 1 addition & 1 deletion docs/libp2p.tools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Subpackages
.. toctree::
:maxdepth: 4

libp2p.tools.async_service
libp2p.tools.anyio_service
libp2p.tools.timed_cache

Submodules
Expand Down
8 changes: 4 additions & 4 deletions examples/pubsub/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
MPLEX_PROTOCOL_ID,
Mplex,
)
from libp2p.tools.async_service.trio_service import (
background_trio_service,
from libp2p.tools.anyio_service.anyio_service import (
background_anyio_service,
)

# Configure logging
Expand Down Expand Up @@ -150,8 +150,8 @@ async def run(topic: str, destination: Optional[str], port: Optional[int]) -> No
logger.info(f"Node started with peer ID: {host.get_id()}")
logger.info(f"Listening on: {listen_addr}")
logger.info("Initializing PubSub and GossipSub...")
async with background_trio_service(pubsub):
async with background_trio_service(gossipsub):
async with background_anyio_service(pubsub):
async with background_anyio_service(gossipsub):
logger.info("Pubsub and GossipSub services started.")
await pubsub.wait_until_ready()
logger.info("Pubsub ready.")
Expand Down
2 changes: 1 addition & 1 deletion libp2p/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from libp2p.pubsub.pb import (
rpc_pb2,
)
from libp2p.tools.async_service import (
from libp2p.tools.anyio_service import (
ServiceAPI,
)

Expand Down
6 changes: 3 additions & 3 deletions libp2p/host/basic_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
from libp2p.protocol_muxer.multiselect_communicator import (
MultiselectCommunicator,
)
from libp2p.tools.async_service import (
background_trio_service,
from libp2p.tools.anyio_service import (
background_anyio_service,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -157,7 +157,7 @@ async def run(
:param listen_addrs: a sequence of multiaddrs that we want to listen to
"""
network = self.get_network()
async with background_trio_service(network):
async with background_anyio_service(network):
await network.listen(*listen_addrs)
yield

Expand Down
2 changes: 1 addition & 1 deletion libp2p/network/swarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from libp2p.peer.peerstore import (
PeerStoreError,
)
from libp2p.tools.async_service import (
from libp2p.tools.anyio_service import (
Service,
)
from libp2p.transport.exceptions import (
Expand Down
2 changes: 1 addition & 1 deletion libp2p/pubsub/gossipsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from libp2p.pubsub import (
floodsub,
)
from libp2p.tools.async_service import (
from libp2p.tools.anyio_service import (
Service,
)
from libp2p.utils import (
Expand Down
2 changes: 1 addition & 1 deletion libp2p/pubsub/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
from libp2p.peer.id import (
ID,
)
from libp2p.tools.async_service import (
from libp2p.tools.anyio_service import (
Service,
)
from libp2p.tools.timed_cache.last_seen_cache import (
Expand Down
63 changes: 63 additions & 0 deletions libp2p/tools/anyio_service/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Anyio Service Implementation

This module provides a robust async service implementation based on the anyio library. It offers a modern, actively maintained alternative to the previous async service implementation.

## Key Features

- Modern async primitives from anyio
- Full API compatibility with existing service implementations
- Improved performance and memory efficiency
- No task count limitations
- Robust error handling and task management
- Clean service lifecycle management

## Usage

```python
from libp2p.tools.anyio_service import Service, background_anyio_service

class MyService(Service):
async def run(self):
# Your service logic here
pass

# Run service in background
async with background_anyio_service(MyService()) as manager:
# Service is running
pass
# Service is automatically cleaned up

# Or run service blocking
await AnyioManager.run_service(MyService())
```

## API

The implementation maintains the same public API as the previous async service implementation:

- `Service` - Base class for all services
- `ServiceAPI` - Interface defining service behavior
- `ManagerAPI` - Interface for service management
- `background_anyio_service()` - Context manager for running services
- `as_service()` - Decorator to create services from functions

## Benefits

- Eliminates reliance on unmaintained external codebase
- Leverages anyio's robust async primitives
- Reduces technical debt
- Improves maintainability
- Better error handling and task management
- No artificial task count limitations

## Migration

To migrate from the previous async service implementation:

1. Update imports to use `libp2p.tools.anyio_service` instead of `libp2p.tools.async_service`
1. No other code changes required - the API is fully compatible

## Requirements

- Python 3.7+
- anyio library
25 changes: 25 additions & 0 deletions libp2p/tools/anyio_service/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from .abc import (
ServiceAPI,
)
from .anyio_service import (
AnyioManager,
background_anyio_service,
)
from .base import (
Service,
as_service,
)
from .exceptions import (
DaemonTaskExit,
LifecycleError,
)

__all__ = [
"ServiceAPI",
"Service",
"as_service",
"DaemonTaskExit",
"LifecycleError",
"AnyioManager",
"background_anyio_service",
]
Loading
Loading