Coming from aiomysql or asyncmy¶
rustmy.aio keeps their API: connect() and create_pool() awaited or
used with async with, await conn.cursor() (or plain conn.cursor()), the
same coroutines on connections and cursors, a plain close() and an awaited
ensure_closed(), autocommit off.
from rustmy import aio # instead of: import aiomysql / import asyncmy
pool = await aio.create_pool(host="localhost", user="app", password="secret", db="shop")
async with pool.acquire() as conn:
async with conn.cursor(aio.DictCursor) as cursor:
await cursor.execute("SELECT id, name FROM products WHERE id = %s", (42,))
row = await cursor.fetchone()
What differs, on purpose¶
- Fetching after a statement that returned no rows (an
INSERT, DDL) raisesProgrammingError, as PEP 249 asks; both return[]fromfetchall()andNonefromfetchone(). - Server errors are classed by SQLSTATE, as in the blocking API: "unknown
column" (1054) is a
ProgrammingError, where both raiseOperationalError. - A task cancelled in the middle of a command closes its connection, as in
both; the next command on it raises
OperationalError(2013) instead ofInterfaceError. - Pools serve waiting tasks in order of arrival (see
Connection pools),
acquire()takes atimeout, and a connection idle for 30 seconds is pinged before it is lent (pre_ping).pool_recycleis off by default, as in aiomysql (asyncmy recycles after an hour), and there is noecho. - The rest follows rustmy's blocking API, so the differences in Coming from mysqlclient or PyMySQL apply too.
With SQLAlchemy¶
create_async_engine("mysql+rustmy://...") replaces
mysql+aiomysql:// and mysql+asyncmy://: see
SQLAlchemy.