liveBook · Manning

Sock_accept

As explained in Python Concurrency with asyncio

sock_accept is an asyncio event-loop coroutine method that asynchronously accepts an incoming connection from a supplied non-blocking socket and returns the connection and client address.

sock_accept is an asyncio event-loop coroutine method for accepting an incoming connection without blocking. Pass it a non-blocking socket already bound to a port, await the returned coroutine, and receive a connection socket together with the client address. section 3.5.1

Use sock_accept as the first step in an asynchronous socket server:

  • Create, bind, listen on, and configure the server socket as non-blocking. section 3.5.1
  • Await sock_accept with the server socket, then handle the returned connection and client address. section 3.5.1
  • Continue with sock_recv and sock_sendall to receive data and send a complete response asynchronously. section 3.5.1

Waiting for a client

The basic call is `connection, address = await loop.sock_accept(socket)`. A selector-based implementation registers the server socket for read events, pauses on a future, and resumes when a client connects. section 3.5.1 section 14.5.4

connection, address = await loop.sock_accept(socket)

Inspect the minimal call pattern for accepting a connection and unpacking its result. section 3.5.1

How Python Concurrency with asyncio covers it

How deep it goes
Explains the awaitable result, then descends into futures, selectors, callbacks, and event-loop scheduling.
Examples
Uses a listening socket, a Telnet connection, selector callbacks, and a custom future to demonstrate acceptance.
What it assumes
Assumes basic socket operations while requiring a bound, listening, non-blocking socket.
What you can do after
Apply it via asyncio to accept clients without blocking the event loop.
How it connects
Connects asyncio event loops with coroutines, sockets, futures, selectors, and tasks.

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.