{"id":5118,"library":"asyncmy","title":"asyncmy - A fast asyncio MySQL Driver","description":"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.","status":"active","version":"0.2.11","language":"en","source_language":"en","source_url":"https://github.com/long2ice/asyncmy.git","tags":["async","mysql","database","driver","mariadb","asyncio"],"install":[{"cmd":"pip install asyncmy","lang":"bash","label":"Standard Install"},{"cmd":"pip install asyncmy # On Windows, requires Microsoft C++ Build Tools","lang":"bash","label":"Windows Install"}],"dependencies":[],"imports":[{"symbol":"connect","correct":"from asyncmy import connect"},{"note":"For managing multiple database connections efficiently.","symbol":"create_pool","correct":"from asyncmy import create_pool"},{"note":"A cursor that returns rows as dictionaries.","symbol":"DictCursor","correct":"from asyncmy.cursors import DictCursor"},{"note":"For working with MySQL's replication protocol.","symbol":"BinLogStream","correct":"from asyncmy.replication import BinLogStream"}],"quickstart":{"code":"import asyncio\nimport os\nfrom asyncmy import connect\nfrom asyncmy.cursors import DictCursor\n\nasync def main():\n    conn = await connect(\n        host=os.environ.get('MYSQL_HOST', '127.0.0.1'),\n        user=os.environ.get('MYSQL_USER', 'root'),\n        password=os.environ.get('MYSQL_PASSWORD', ''),\n        db=os.environ.get('MYSQL_DB', 'test_db'),\n        port=int(os.environ.get('MYSQL_PORT', 3306))\n    )\n    try:\n        async with conn.cursor(cursor=DictCursor) as cursor:\n            await cursor.execute(\"CREATE DATABASE IF NOT EXISTS test_db\")\n            await cursor.execute(\"USE test_db\")\n            await cursor.execute(\n                \"\"\"\n                CREATE TABLE IF NOT EXISTS users (\n                    id INT PRIMARY KEY AUTO_INCREMENT,\n                    name VARCHAR(255),\n                    email VARCHAR(255)\n                )\n                \"\"\"\n            )\n            await cursor.execute(\n                \"INSERT INTO users (name, email) VALUES (%s, %s)\",\n                (\"Alice\", \"alice@example.com\")\n            )\n            await cursor.execute(\"SELECT id, name, email FROM users WHERE name = %s\", (\"Alice\",))\n            result = await cursor.fetchone()\n            print(f\"Fetched user: {result}\")\n    finally:\n        await conn.close()\n\nif __name__ == '__main__':\n    asyncio.run(main())\n","lang":"python","description":"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."},"warnings":[{"fix":"Download and install Microsoft C++ Build Tools from the Visual Studio website before running `pip install asyncmy`.","message":"On Windows, `asyncmy` uses Cython extensions which require Microsoft C++ Build Tools to be installed for successful installation via pip. Without these tools, installation may fail or result in a non-optimized pure-Python fallback.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"Always ensure connections and cursors are properly closed to prevent resource leaks. While `async with` statements handle this for contexts, direct `connect()` calls require an explicit `await conn.close()`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"Common `asyncio` pitfalls like 'fire and forget' tasks (not awaiting coroutines) or blocking the event loop with synchronous operations can lead to unexpected behavior, lost exceptions, or reduced performance. `asyncmy` operations are all awaitable and should be used within an `asyncio` event loop.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Remove the `loop=asyncio.get_event_loop()` argument from `connect()` and `create_pool()` calls. `asyncmy` will automatically use the current running event loop.","message":"The `loop` argument for `asyncmy.connect()` and `asyncmy.create_pool()` was removed in version 0.2.1, as passing an explicit event loop is generally no longer necessary or recommended in modern `asyncio`.","severity":"breaking","affected_versions":">=0.2.1"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}