asyncmy is a high-performance asynchronous MySQL/MariaDB driver for Python, leveraging `asyncio`. It reuses much of the `PyMySQL` and `aiomysql` codebase but significantly boosts performance by rewriting its core protocol in Cython. The library offers an API compatible with `aiomysql` and supports advanced features like the MySQL replication protocol. It is actively maintained, with releases typically occurring as new features or performance improvements are integrated.
pip install asyncmyVerified import paths — ran on the pinned version, not inferred.
This example demonstrates connecting to a MySQL database, executing DDL and DML operations, and fetching results using a DictCursor. It uses environment variables for connection parameters and ensures proper resource cleanup using `async with` and a `finally` block.
Download and install Microsoft C++ Build Tools from the Visual Studio website before running `pip install asyncmy`.
Use `async with conn.cursor(...)` and `async with asyncmy.create_pool(...)` where possible. For direct `connect()`, always pair with `await conn.close()` in a `finally` block.
Ensure all `async` functions are `await`ed. Use `asyncio.create_task()` for background tasks and `asyncio.gather()` for concurrent execution. Avoid blocking I/O or CPU-bound synchronous calls directly in the event loop.
Remove the `loop=asyncio.get_event_loop()` argument from `connect()` and `create_pool()` calls. `asyncmy` will automatically use the current running event loop.
Ensure you're using an asynchronous driver by modifying your database URL to include 'asyncmy', e.g., 'mysql+asyncmy://user:password@localhost/dbname'. Additionally, install the required packages: 'pip install asyncmy sqlalchemy[asyncio]'.
Upgrade your database to MySQL 5.7.8+ or MariaDB 10.2.7+ to ensure JSON data type support.
Increase the 'maxsize' parameter of your connection pool configuration to allow more connections, and ensure that connections are properly released after use by utilizing context managers.
Ensure that all calls to `async` functions are preceded by `await` within another `async` function, or run directly using `asyncio.run(main_coroutine())` for the top-level entry point.
Implement a connection pool with `pool_recycle` set to a value less than the MySQL or proxy's idle timeout. For SQLAlchemy, configure `create_async_engine(..., pool_recycle=seconds)`.
No dependency data recorded yet.