Skip to content

Pool

For tasks, see rustmy.aio.create_pool.

Pool

Pool(
    minsize: int = 1,
    maxsize: int = 10,
    *,
    pool_recycle: float = -1,
    idle_timeout: float | None = None,
    pre_ping: float | None = 30.0,
    reset_on_release: bool = False,
    **connect_args: Any,
)

Connections opened with rustmy.connect()'s arguments (connect_args), lent to one thread at a time.

minsize connections are opened at once and kept open; more are opened as they are needed, up to maxsize, after which acquire() waits (in order of arrival) for one to be given back. The idle ones are lent most recently used first.

A connection given back with a transaction open is rolled back; with reset_on_release, it is also reset (Connection.reset_connection(): session variables and temporary tables go, a round trip per use). One that is closed, fails, or is older than pool_recycle seconds is closed instead of lent again; one idle for pre_ping seconds is pinged before it is lent; and idle ones beyond minsize are closed after idle_timeout seconds unused (checked as connections are acquired and given back).

with Pool(...) as pool closes the pool at the end of the block and waits for the connections in use to come back.

minsize property

minsize: int

maxsize property

maxsize: int

size property

size: int

Connections open, in use or idle, or being opened.

freesize property

freesize: int

Idle connections.

closed property

closed: bool

acquire

acquire(timeout: float | None = None) -> _Lease

Lend a connection: with pool.acquire() as conn:.

Waits up to timeout seconds (forever with None) when every connection is in use, then raises PoolTimeout. Raises InterfaceError once the pool is closed, and what rustmy.connect() raises when a connection cannot be opened.

release

release(conn: Connection) -> None

Give conn back (acquire's with block does it).

clear

clear() -> None

Close the idle connections (others are opened for the minimum).

close

close() -> None

Close the pool: idle connections now, those in use as they come back; waiting and later acquire() calls raise.

terminate

terminate() -> None

Close the pool and every connection, those in use included (their threads see them closed).

wait_closed

wait_closed(timeout: float | None = None) -> bool

Wait for the connections in use to come back once the pool is closed; false if timeout seconds pass first.