• Stars
    star
    234
  • Rank 170,997 (Top 4 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 3 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A fast asyncio MySQL/MariaDB driver with replication protocol support

asyncmy - A fast asyncio MySQL/MariaDB driver

image image pypi ci

Introduction

asyncmy is a fast asyncio MySQL/MariaDB driver, which reuse most of pymysql and aiomysql but rewrite core protocol with cython to speedup.

Features

  • API compatible with aiomysql.
  • Faster by cython.
  • MySQL replication protocol support with asyncio.
  • Tested both MySQL and MariaDB in CI.

Benchmark

The result comes from benchmark.

The device is iMac Pro(2017) i9 3.6GHz 48G and MySQL version is 8.0.26.

benchmark

Conclusion

  • There is no doubt that mysqlclient is the fastest MySQL driver.
  • All kinds of drivers have a small gap except select.
  • asyncio could enhance insert.
  • asyncmy performs remarkable when compared to other drivers.

Install

pip install asyncmy

Installing on Windows

To install asyncmy on Windows, you need to install the tools needed to build it.

  1. Download Microsoft C++ Build Tools from https://visualstudio.microsoft.com/visual-cpp-build-tools/
  2. Run CMD as Admin (not required but recommended) and navigate to the folder when your installer is downloaded
  3. Installer executable should look like this vs_buildtools__XXXXXXXXX.XXXXXXXXXX.exe, it will be easier if you rename it to just vs_buildtools.exe
  4. Run this command (Make sure you have about 5-6GB of free storage)
vs_buildtools.exe --norestart --passive --downloadThenInstall --includeRecommended --add Microsoft.VisualStudio.Workload.NativeDesktop --add Microsoft.VisualStudio.Workload.VCTools --add Microsoft.VisualStudio.Workload.MSBuildTools
  1. Wait until the installation is finished
  2. After installation will finish, restart your computer
  3. Install asyncmy via PIP
pip install asyncmy

Now you can uninstall previously installed tools.

Usage

Use connect

asyncmy provides a way to connect to MySQL database with simple factory function asyncmy.connect(). Use this function if you want just one connection to the database, consider connection pool for multiple connections.

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())

Use pool

asyncmy provides connection pool as well as plain Connection objects.

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

asyncmy supports MySQL replication protocol like python-mysql-replication, but powered by asyncio.

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 build on top of these awesome projects.

  • pymysql, a pure python MySQL client.
  • aiomysql, a library for accessing a MySQL database from the asyncio.
  • python-mysql-replication, pure Python Implementation of MySQL replication protocol build on top of PyMYSQL.

License

This project is licensed under the Apache-2.0 License.

More Repositories

1

fastapi-cache

fastapi-cache is a tool to cache fastapi response and function result, with backends support redis and memcached.
Python
1,264
star
2

fastapi-limiter

A request rate limiter for fastapi
Python
452
star
3

synch

Sync data from the other DB to ClickHouse(cluster)
Python
345
star
4

meilisync

Realtime sync data from MySQL/PostgreSQL/MongoDB to Meilisearch
Python
230
star
5

asynch

An asyncio ClickHouse Python Driver with native (TCP) interface support.
Python
169
star
6

rearq

A distributed task queue built with asyncio and redis, with built-in web interface
Python
147
star
7

swagin

Swagger + Gin = SwaGin, a web framework based on Gin and Swagger
Go
68
star
8

trader

A framework that automated cryptocurrency exchange with strategy
Go
62
star
9

databack

Backup your data from MySQL/PostgreSQL/SSH etc. to any other storages
Python
60
star
10

longurl

A self-hosted short url service
Go
51
star
11

meilisync-admin

A web admin dashboard for meilisync
Python
31
star
12

fibers

Fiber + Swagger = Fibers, a web framework dedicated to providing a FastAPI-like development experience
Go
26
star
13

alarmer

A tool focus on error reporting for your application, like sentry but lightweight
Python
19
star
14

fastapi-rest

Fast restful API based on FastAPI and TortoiseORM
Python
10
star
15

gema-web

Convert from json/xml/yaml to Pydantic/Go/Rust etc.
TypeScript
9
star
16

awesome-web

Search awesome projects
TypeScript
8
star
17

chcli

A Terminal Client for ClickHouse with AutoCompletion and Syntax Highlighting.
Python
6
star
18

fettler

Auto refresh cache of redis with MySQL binlog
Python
4
star
19

telsearch-web

Frontend for telsearch
TypeScript
3
star
20

mccabe

Calculate the cyclomatic complexity of the source code
Python
3
star
21

s3web

Serve static files from any S3 compatible object storage endpoints
Go
2
star
22

ClashForiOS

Swift
2
star
23

xiaoai

小爱音箱非官方SDK
Python
2
star
24

awesome

Search for awesome github project
Go
2
star
25

longurl-web

This is frontend for https://github.com/long2ice/longurl
TypeScript
2
star
26

ergo

Python
2
star
27

gema

Convert from json/xml/yaml to Pydantic/Go/Rust etc.
Python
2
star
28

nvim

My personal neovim config
Lua
2
star
29

sponsor

Sponsor page of long2ice
HTML
2
star
30

youtube-dl-api-server

api server for youtube-dl
Python
2
star
31

talkit

A self hosted comment system
1
star
32

long2ice

My Personal README.
1
star
33

vpsmon

Python
1
star
34

homepage

My homepage
TypeScript
1
star
35

kanp

See video by download
Python
1
star
36

devme

Python
1
star
37

creatable

A tool to create table from file/database to another database
Python
1
star
38

devme-web

TypeScript
1
star
39

hugo-theme-pure

Forked from https://github.com/xiaoheiAh/hugo-theme-pure and make improvements
CSS
1
star
40

fastapi-monitor

Python
1
star
41

meilisync-web

Frontend of meilisearch-admin
Vue
1
star