Exploring asyncmy: A Fast asyncio MySQL/MariaDB Driver
Asynchronous programming has become increasingly popular for building scalable and high-performance applications. When it comes to accessing MySQL or MariaDB databases with asyncio, asyncmy emerges as a powerful and efficient choice. In this article, we will dive into the world of asyncmy, exploring its features, performance benchmarks, installation process, and usage examples.
Introduction
asyncmy is a fast asyncio MySQL/MariaDB driver that inherits most of its functionality from the popular libraries pymysql and aiomysql. However, what sets asyncmy apart is its core protocol implementation in Cython, resulting in significant speed improvements.
Key Features
- API compatibility with aiomysql ensures an easy transition for developers already familiar with it.
- The use of Cython makes asyncmy faster compared to other drivers.
- asyncmy has built-in support for the MySQL replication protocol, making it suitable for applications requiring database replication.
- The driver has been extensively tested with both MySQL and MariaDB in continuous integration environments.
Benchmark Results
The benchmark results for asyncmy, along with other MySQL drivers, highlight its superior performance. Tested on an iMac Pro with an i9 processor and 48GB of RAM, asyncmy exhibited remarkable speed, especially in comparison to other drivers. The benchmark graph showcases the significant performance gains achieved by asyncmy.
Installation
Installing asyncmy is straightforward. Simply use pip to install the library:
shell
pip install asyncmy
For Windows users, additional steps are required. Download and install the Microsoft C++ Build Tools from the official Microsoft website. Once installed, you can proceed with the pip installation.
Usage Examples
asyncmy provides two primary methods to establish database connections: connect
and pool
. The connect
function is suitable for single connections, while the pool
method allows for connection pooling.
Here is an example of connecting to a MySQL database using connect
:
“`python
from asyncmy import connect
from asyncmy.cursors import DictCursor
import asyncio
async def run():
conn = await connect()
async with conn.cursor(cursor=DictCursor) as cursor:
await cursor.execute(“create database if not exists test”)
await cursor.execute(“””CREATE TABLE if not exists test.asyncmy (
id
int primary key auto_increment,
decimal
decimal(10, 2),
date
date,
datetime
datetime,
float
float,
string
varchar(200),
tinyint
tinyint
)”””)
if name == ‘main‘:
asyncio.run(run())
“`
To connect using connection pooling, you can use the following code:
“`python
import asyncmy
import asyncio
async def run():
pool = await asyncmy.create_pool()
async with pool.acquire() as conn:
async with conn.cursor() as cursor:
await cursor.execute(“SELECT 1”)
ret = await cursor.fetchone()
assert ret == (1,)
if name == ‘main‘:
asyncio.run(run())
“`
Replication Support
asyncmy also offers support for MySQL replication protocol, similar to the python-mysql-replication library. However, asyncmy leverages the power of asyncio, making it efficient and well-suited for applications that require real-time replication capabilities.
“`python
from asyncmy import connect
from asyncmy.replication import BinLogStream
import asyncio
async def run():
conn = await connect()
ctl_conn = await connect()
stream = BinLogStream(
conn,
ctl_conn,
1,
master_log_file="binlog.000172",
master_log_position=2235312,
resume_stream=True,
blocking=True,
)
async for event in stream:
print(event)
if name == ‘main‘:
asyncio.run(run())
“`
ThanksTo
asyncmy is built on top of several fantastic projects, including:
– pymysql: A pure Python MySQL client.
– aiomysql: A library for accessing a MySQL database from asyncio.
– python-mysql-replication: A pure Python implementation of the MySQL replication protocol built on top of PyMYSQL.
Conclusion
asyncmy is a game-changing asyncio MySQL/MariaDB driver that combines the best features of pymysql and aiomysql while introducing notable performance improvements. With its support for the replication protocol and efficient Cython implementation, asyncmy is a valuable tool for developers seeking high-performance database access with asyncio.
If you’re looking to supercharge your asyncio MySQL/MariaDB applications, asyncmy is definitely worth exploring. Install it today and start building faster, more robust applications with ease.
License
asyncmy is licensed under the Apache-2.0 License.
Leave a Reply