liveBook · Manning

Acquire

As explained in Python Concurrency with asyncio

acquire is an asyncpg coroutine that obtains an available database connection from a connection pool and waits if necessary.

acquire is an asyncpg coroutine operation that obtains an available database connection from a connection pool. If every connection is busy, it waits until one is released. Reusing pooled connections avoids the cost of creating a new connection for each query. section 5.5.2

Acquire a connection for one query

Use `async with pool.acquire() as connection:` to borrow a connection, execute `await connection.fetchrow(...)`, and return the connection automatically when the block ends. section 5.5.2

Figure 5.2 Coroutines 1 and 2 acquire connections to run their queries while Coroutine 3 waits for a connection. Once either Coroutine 1 or 2 finishes, Coroutine 3 will be able to use the newly released connection and will be able to execute its query.
05-02

A pool with two connections lets the first two query coroutines proceed while a third waits; releasing either connection lets the third continue. section 5.5.2

How Python Concurrency with asyncio covers it

How deep it goes
Explains borrowing, waiting, release, and reuse, then demonstrates acquisition in an async query function.
Examples
Uses a two-connection pool, three query coroutines, and an asyncpg product-query function.
What it assumes
Assumes familiarity with async coroutines and queries while establishing connection-pool reuse and waiting from first principles.
What you can do after
Apply it via asyncpg to run queries concurrently while returning borrowed connections for reuse.
How it connects
Connects connection pools, coroutines, asynchronous queries, concurrency, and connection release.

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.