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.
encoders
instance-attribute
¶
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.
cursor
¶
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().
escape
¶
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
¶
Return the session to its just-connected state; as
rustmy.connections.Connection.reset_connection().
change_user
async
¶
Re-authenticate as another account on this connection; as
rustmy.connections.Connection.change_user().
set_character_set
async
¶
Switch the session to charset (and collation, if given);
as rustmy.connections.Connection.set_character_set().
close
¶
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).
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
¶
Rows fetchmany() returns by default (PEP 249).
max_stmt_length
class-attribute
instance-attribute
¶
Largest statement executemany() builds when batching inserts (as rustmy.cursors.Cursor.max_stmt_length).
max_pipeline
class-attribute
instance-attribute
¶
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.
execute
async
¶
Run query, interpolating args if given; return the rowcount.
Without args the query is sent as is, so % needs no escaping.
executemany
async
¶
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
¶
Call the stored procedure procname with args; as
rustmy.cursors.Cursor.callproc().
scroll
¶
Move to another row; as rustmy.cursors.Cursor.scroll().
DictCursor
¶
DictCursor(connection: Connection)
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.
execute
async
¶
Run query, interpolating args if given; return the rowcount
(-1 for a result set, whose size is unknown until the end).
executemany
async
¶
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.
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
¶
Skip forward to another row (see
rustmy.cursors.SSCursor.scroll()): a coroutine here, since
skipping may read from the server.
SSDictCursor
¶
SSDictCursor(connection: Connection)
PreparedCursor
¶
PreparedCursor(connection: Connection)
Bases: Cursor
Buffered cursor running server-side prepared statements; as
rustmy.cursors.PreparedCursor.
max_stmt_length
class-attribute
instance-attribute
¶
PreparedDictCursor
¶
PreparedDictCursor(connection: Connection)
Bases: PreparedCursor
PreparedCursor returning each row as a dict.
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.
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.
close
¶
Close the pool: idle connections now, those in use as they come
back; waiting and later acquire() calls raise.
wait_closed
async
¶
Wait for the connections in use to come back once the pool is closed.