liveBook · Manning

Sock_recv

As explained in Python Concurrency with asyncio

sock_recv is an asyncio event-loop coroutine method that asynchronously receives bytes from a supplied socket when they are available.

sock_recv is an event-loop coroutine that accepts a socket and suspends with await until bytes are available to process. It provides the asynchronous counterpart to a socket receive operation. section 3.5.1

The socket must be non-blocking. Internally, an event loop can register it for read events, associate a callback and future, then resume the coroutine when the selector reports available data. section 3.5.1 section 14.5.6

Receiving and echoing data

data = await loop.sock_recv(socket) success = await loop.sock_sendall(socket, data) section 3.5.1

How Python Concurrency with asyncio covers it

How deep it goes
Explains awaiting and non-blocking use, then traces registration, callbacks, futures, selectors, and coroutine resumption.
Examples
Uses an awaited receive followed by an awaited send to illustrate processing and echoing socket data.
What it assumes
Assumes familiarity with sockets and introduces their coroutine-based event-loop counterparts through direct analogy.
What you can do after
Apply it via an asyncio event loop to await incoming socket bytes without blocking the coroutine-based application.
How it connects
Connects sockets with event loops, coroutines, tasks, futures, callbacks, selectors, and asynchronous sending.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.