Skip to content

rustmy.aio

rustmy for asyncio: connect() opens a connection on the running event loop, with the arguments of rustmy.connect().

The API has the shape of aiomysql's and asyncmy's: await cur.execute(...), await cur.fetchall(), async with conn.cursor() as cur, await conn.commit(). The protocol runs in Rust on the loop's thread.

One difference from those drivers: fetching after a statement that returned no result set (an INSERT, DDL) raises ProgrammingError, as PEP 249 asks and as rustmy's blocking cursors do, instead of returning ().

Connecting

rustmy.aio.connect

connect(
    host: str | None = None,
    user: str | None = None,
    password: str | None = None,
    database: str | None = None,
    port: int | None = None,
    unix_socket: PathArg | None = None,
    *,
    cursorclass: type[Cursor] = Cursor,
    conv: Mapping[Any, Any] | None = None,
    **options: Any,
) -> _Connect

Open a connection on the running event loop: conn = await connect(...), or async with connect(...) as conn, which closes it at the end of the block.

Takes the arguments of rustmy.connect(), with the same defaults and meaning (passwd, db, ssl=, option files, init_command, autocommit...), in the same positional order. connect_timeout (default 15 seconds, 0 for none) bounds opening the connection, TLS and authentication included. cursorclass is one of rustmy.aio.cursors.

rustmy.aio.Connection

An asyncio connection to a MySQL or MariaDB server (rustmy.aio.connect() opens one).

One command at a time: starting another before the first is awaited raises ProgrammingError. A command whose task is cancelled (or times out) closes the connection: the server goes on with the statement, and its reply would arrive in the middle of the next one.

cursorclass instance-attribute

cursorclass = cursorclass

encoders instance-attribute

encoders: dict[type, Encoder] = dict(default_encoders)

Python types to SQL literal encoders (MySQLdb's attribute), from conv; escape() takes it as its mapping.

no_backslash_escapes property

no_backslash_escapes: bool

Whether the session has NO_BACKSLASH_ESCAPES in its SQL mode.

closed property

closed: bool

tls property

tls: bool

Whether the connection is encrypted with TLS.

compression property

compression: str | None

Compression in use: "zlib", "zstd" or None.

open property

open: bool

encoding property

encoding: str

Python codec of the session's character set.

cursor

cursor(
    cursorclass: None = None,
    *,
    cursor: None = None,
    prepared: bool = False,
) -> Cursor
cursor(
    cursorclass: type[_CursorT],
    *,
    cursor: None = None,
    prepared: bool = False,
) -> _CursorT
cursor(
    cursorclass: None = None,
    *,
    cursor: type[_CursorT],
    prepared: bool = False,
) -> _CursorT
cursor(
    cursorclass: type[Cursor] | None = None,
    *,
    cursor: type[Cursor] | None = None,
    prepared: bool = False,
) -> Cursor

Create a cursor of cursorclass, by default the one given to connect(); as rustmy.connections.Connection.cursor().

begin async

begin() -> None

commit async

commit() -> None

rollback async

rollback() -> None

autocommit async

autocommit(on: bool) -> None

Enable or disable autocommit mode.

get_autocommit

get_autocommit() -> bool

escape_string

escape_string(value: str) -> str

literal

literal(value: object) -> str

value as an SQL literal for this connection.

escape

escape(value: object, mapping: Mapping[Any, Any] | None = None) -> str

value as an SQL literal; as rustmy.connections.Connection.escape().

ping async

ping(reconnect: bool = False) -> None

Check that the server is reachable; raises if it is not.

reconnect (aiomysql's) is accepted for compatibility but not supported: a connection that broke is not reopened.

select_db async

select_db(database: str) -> None

Make database the default database of the session.

reset_connection async

reset_connection() -> None

Return the session to its just-connected state; as rustmy.connections.Connection.reset_connection().

change_user async

change_user(
    user: str, password: str = "", database: str | None = None
) -> None

Re-authenticate as another account on this connection; as rustmy.connections.Connection.change_user().

set_character_set async

set_character_set(charset: str, collation: str | None = None) -> None

Switch the session to charset (and collation, if given); as rustmy.connections.Connection.set_character_set().

close

close() -> None

Close the connection, sending COM_QUIT first when no command is running. Closing it again raises InterfaceError, as PEP 249 asks (leaving an async with block does not).

ensure_closed async

ensure_closed() -> None

Close the connection (aiomysql's and asyncmy's name).

thread_id

thread_id() -> int

Server-side connection id.

get_server_info

get_server_info() -> str

character_set_name

character_set_name() -> str

The session's character set, as MySQL names it.

Cursors

asyncio cursors, as rustmy.cursors with await.

execute, callproc, nextset, close and the fetch* methods are coroutines, as in aiomysql and asyncmy: await cursor.fetchall() works the same with every cursor. Buffered cursors read the whole result in execute, converting rows in batches as they arrive.

Cursor

Cursor(connection: Connection)

Buffered cursor: execute reads the whole result.

arraysize class-attribute instance-attribute

arraysize = 1

Rows fetchmany() returns by default (PEP 249).

max_stmt_length class-attribute instance-attribute

max_stmt_length = 1024000

Largest statement executemany() builds when batching inserts (as rustmy.cursors.Cursor.max_stmt_length).

max_pipeline class-attribute instance-attribute

max_pipeline = 256

Statements executemany() sends before reading their replies, when it cannot batch them into one (anything but INSERT … VALUES). 1 sends each one after the previous reply.

description instance-attribute

description: Any = None

rowcount instance-attribute

rowcount = -1

lastrowid instance-attribute

lastrowid: int | None = None

connection property

connection: Connection

rownumber property

rownumber: int | None

close async

close() -> None

mogrify

mogrify(query: str, args: Params = None) -> str

Return the query exactly as execute() would send it.

execute async

execute(query: str, args: Params = None) -> int

Run query, interpolating args if given; return the rowcount.

Without args the query is sent as is, so % needs no escaping.

executemany async

executemany(query: str, seq_of_args: Iterable[Params]) -> int

Run query once per item of seq_of_args; as rustmy.cursors.Cursor.executemany().

A single-row INSERT/REPLACE … VALUES (…) is sent as multi-row statements of at most max_stmt_length bytes; anything else runs once per item, pipelined in windows of up to max_pipeline statements (and 16 KiB). Items are taken from seq_of_args as each statement or window is built, and an error stops the batch at the end of its window. Returns the total number of affected rows; the cursor keeps the last statement's result.

callproc async

callproc(procname: str, args: Sequence[Any] = ()) -> Sequence[Any]

Call the stored procedure procname with args; as rustmy.cursors.Cursor.callproc().

nextset async

nextset() -> bool | None

Advance to the next result set of a multi-result query.

fetchone async

fetchone() -> Row | None

fetchmany async

fetchmany(size: int | None = None) -> tuple[Row, ...]

fetchall async

fetchall() -> tuple[Row, ...]

scroll

scroll(value: int, mode: str = 'relative') -> None

Move to another row; as rustmy.cursors.Cursor.scroll().

setinputsizes

setinputsizes(sizes: object) -> None

No-op, required by PEP 249.

setoutputsize

setoutputsize(size: object, column: object = None) -> None

No-op, required by PEP 249.

DictCursor

DictCursor(connection: Connection)

Bases: Cursor

Buffered cursor returning each row as a dict keyed by column name; as rustmy.cursors.DictCursor.

fetchone async

fetchone() -> DictRow | None

fetchmany async

fetchmany(size: int | None = None) -> tuple[DictRow, ...]

fetchall async

fetchall() -> tuple[DictRow, ...]

SSCursor

SSCursor(connection: Connection)

Bases: Cursor

Unbuffered cursor: rows stay on the server until they are fetched; as rustmy.cursors.SSCursor.

Rows are read off the event loop one batch at a time, and only when more are asked for than the batch at hand: memory stays flat whatever the size of the result set, and a slow consumer keeps the server waiting (past its net_write_timeout, 60 seconds by default, it drops the connection). Another command on the connection discards the rows not yet fetched, with a UserWarning; fetching afterwards raises ProgrammingError. rowcount is -1 for result sets, and scroll() only moves forward.

rownumber property

rownumber: int | None

close async

close() -> None

execute async

execute(query: str, args: Params = None) -> int

Run query, interpolating args if given; return the rowcount (-1 for a result set, whose size is unknown until the end).

executemany async

executemany(query: str, seq_of_args: Iterable[Params]) -> int

As Cursor.executemany(); the last statement's rows are streamed. Its rowcount is -1 when it returns a result set.

nextset async

nextset() -> bool | None

Skip what is left of this result set and move to the next one.

fetchone async

fetchone() -> Row | None

fetchmany async

fetchmany(size: int | None = None) -> tuple[Row, ...]

fetchall async

fetchall() -> tuple[Row, ...]

Fetch every remaining row (iterate the cursor instead to keep memory flat).

fetchall_unbuffered

fetchall_unbuffered() -> AsyncIterator[Row]

Iterate the remaining rows one by one, as iterating the cursor does (aiomysql's and PyMySQL's name).

scroll async

scroll(value: int, mode: str = 'relative') -> None

Skip forward to another row (see rustmy.cursors.SSCursor.scroll()): a coroutine here, since skipping may read from the server.

SSDictCursor

SSDictCursor(connection: Connection)

Bases: SSCursor

Unbuffered cursor returning each row as a dict (see SSCursor and DictCursor).

fetchone async

fetchone() -> DictRow | None

fetchmany async

fetchmany(size: int | None = None) -> tuple[DictRow, ...]

fetchall async

fetchall() -> tuple[DictRow, ...]

PreparedCursor

PreparedCursor(connection: Connection)

Bases: Cursor

Buffered cursor running server-side prepared statements; as rustmy.cursors.PreparedCursor.

max_stmt_length class-attribute instance-attribute

max_stmt_length = 256 * 1024

PreparedDictCursor

PreparedDictCursor(connection: Connection)

Bases: PreparedCursor

PreparedCursor returning each row as a dict.

fetchone async

fetchone() -> DictRow | None

fetchmany async

fetchmany(size: int | None = None) -> tuple[DictRow, ...]

fetchall async

fetchall() -> tuple[DictRow, ...]

Pools

rustmy.aio.create_pool

create_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,
) -> _CreatePool

A pool of connections (Pool): pool = await create_pool(...), or async with create_pool(...) as pool.

minsize connections are opened first (their errors raise here) and kept open; more are opened as needed up to maxsize. connect_args are rustmy.aio.connect()'s.

rustmy.aio.Pool

Connections opened with rustmy.aio.connect()'s arguments, lent to one task at a time; create_pool() makes one. As rustmy.Pool (the same options, and the same health checks: rollback when given back with a transaction open, reset_on_release, pre_ping, pool_recycle, idle_timeout).

A connection is given back with async with pool.acquire() as conn, or with release() (whose task may be awaited). A task cancelled in the middle of a command closes its connection, which is then not lent again.

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) -> _Acquire

Lend a connection: async with pool.acquire() as conn, or conn = await pool.acquire() and release() later.

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.aio.connect() raises when a connection cannot be opened.

release

release(conn: Connection) -> Future[None]

Give conn back: rolled back first if a transaction is open. The returned future may be awaited (async with awaits it); it is already done unless the connection needs a command first.

clear async

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.

wait_closed async

wait_closed() -> None

Wait for the connections in use to come back once the pool is closed.