Skip to content

feat: add basic socket API - #10

Merged
fantix merged 1 commit into
mainfrom
socket
Feb 23, 2026
Merged

fantix merged 1 commit into
mainfrom
socket

Conversation

@fantix

@fantix fantix commented Feb 22, 2026

Copy link
Copy Markdown
Collaborator

Given the completion-based nature of compio, the low-level Python API for I/O in compio-py can be async sockets instead of protocols and transports.

Usage:

import asyncio, socket
import compio

loop = compio.CompioLoop()
asyncio._set_running_loop(loop)

async def main():
    try:
        s = await loop.create_socket(socket.AF_INET)
        print("socket created:", s)

        await s.connect(("www.google.com", 80))

        bytes_sent = await s.send(b"GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n")
        print(f"sent {bytes_sent} bytes")

        data = await s.recv(65536)
        print(f"received {len(data)} bytes")
        print(data[:128])

        await s.shutdown(socket.SHUT_WR)
        await s.close()

    finally:
        loop.stop()

task = asyncio.Task(main(), loop=loop)
loop.run_forever()

@fantix
fantix requested a review from George-Miao February 22, 2026 22:11
@fantix
fantix force-pushed the socket branch 3 times, most recently from b161495 to 7517ef7 Compare February 22, 2026 23:11
Given the completion-based nature of compio, the low-level Python
API for I/O in compio-py can be async sockets instead of protocols
and transports.

@George-Miao George-Miao 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.

LGTM

Comment thread src/socket.rs
) -> PyResult<Bound<'py, PyAny>> {
self.pyloop.bind(py).borrow().spawn_py(py, async move {
let inner = self.inner()?;
let buf = Vec::with_capacity(bufsize);

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.

I'm thinking is it possible to avoid this additional buffer + copy here? Maybe allocate a buffer from python directly and do the IO somehow?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

oh yes, there is recv_into zero-copy variant, I'm just too lazy to get them all out at once. Ty!

@fantix fantix Feb 23, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

For this API specifically, one memory copy is needed anyways because it returns Python bytes which is a fixed-length read-only u8 array. We can alternatively return memoryview in the future from managed buffers, but that would be a nice to have for laters.

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.

Sounds great.

@fantix
fantix merged commit 9861c05 into main Feb 23, 2026
16 checks passed
@fantix
fantix deleted the socket branch February 23, 2026 04:11
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