Skip to content

Add AsyncIO backend - #155

Draft
cgl wants to merge 1 commit into
mainfrom
feat/asyncio-backend
Draft

cgl wants to merge 1 commit into
mainfrom
feat/asyncio-backend

Conversation

@cgl

@cgl cgl commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator
import asyncio
from hip.backends.asyncio import AsyncioBackend

async def send_hello(stream):
    await stream.write(b"hello")

async def main():
    backend = AsyncioBackend()

    async with backend.serve("127.0.0.1", 0, send_hello) as server:
        stream = await backend.connect(server.host, server.port)

        async with stream:
            assert await stream.read() == b"hello"
        assert "CLOSED" in repr(stream)

asyncio.run(main())

#138

@cgl
cgl force-pushed the feat/asyncio-backend branch from 36c17cc to dfd119c Compare November 26, 2026 11:56
@lovelydinosaur

Copy link
Copy Markdown
Member

backend.serve("127.0.0.1", 0, send_hello)

Oh, good reminder that we haven't really got the serve interface right, right?

Eg. I already know what this is gonna do... backend.serve(send_hello).
There's some obvious default args in there, and there's the handler that is mandatory.

@lovelydinosaur lovelydinosaur changed the title Add Asyncio backend Add AsyncIO backend Nov 26, 2026
Comment thread src/hip/backends/asyncio.py Resolved
@cgl
cgl force-pushed the feat/asyncio-backend branch from dfd119c to 9d8ef7e Compare December 1, 2026 11:35
@cgl
cgl requested a review from lovelydinosaur December 1, 2026 11:38
@cgl

cgl commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

backend.serve("127.0.0.1", 0, send_hello)

Yes we can lose the obvious default args in here. 👍🏻

@lovelydinosaur

Copy link
Copy Markdown
Member

Yup, let's get this in... we'll iterate on the low-level interface design as a follow up.

@lovelydinosaur lovelydinosaur left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial review pass...

  • Let's use typing.Self for consistency with other imports.
  • Can we drop the mocking?

import ssl
import types
import typing
from typing import Self

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from typing import Self

except (ConnectionError, OSError): # pragma: no cover
pass

async def __aenter__(self) -> Self:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
async def __aenter__(self) -> Self:
async def __aenter__(self) -> typing.Self:

self._server: asyncio.Server | None = None
self._tasks: set[asyncio.Task[None]] = set()

async def __aenter__(self) -> Self:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
async def __aenter__(self) -> Self:
async def __aenter__(self) -> typing.Self:

Comment on lines +34 to +45
def test_write_empty():
async def run():
reader = AsyncMock()
writer = AsyncMock()
stream = AsyncIOStream(reader, writer)

await stream.write(b"")

writer.write.assert_not_called()
writer.drain.assert_not_awaited()

asyncio.run(run())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we drop the mock test?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants