pyro_mysql

pyro_mysql - High-performance MySQL driver for Python, written in Rust.

import asyncio
import pyro_mysql as mysql

mysql.init(worker_threads=1)

async def example_select():
    conn = await mysql.Conn.new("mysql://localhost@127.0.0.1:3306/test")
    rows = await conn.exec("SELECT * from mydb.mytable")
    print(row[-1].to_dict())


async def example_transaction():
    conn = await mysql.Conn.new("mysql://localhost@127.0.0.1:3306/test")

    async with conn.start_transaction() as tx:
        await tx.exec_drop(
            "INSERT INTO test.asyncmy(`decimal`, `date`, `datetime`, `float`, `string`, `tinyint`) VALUES (?,?,?,?,?,?)",
            (
                1,
                "2021-01-01",
                "2020-07-16 22:49:54",
                1,
                "asyncmy",
                1,
            ),
        )
        await tx.commit()

    await len(conn.exec('SELECT * FROM mydb.mytable')) == 100

# The connection pool is not tied to a single event loop.
# You can reuse the pool between event loops.
asyncio.run(example_pool())
asyncio.run(example_select())
asyncio.run(example_transaction())
...
1from .pyro_mysql import *
2
3__doc__ = pyro_mysql.__doc__
4if hasattr(pyro_mysql, "__all__"):
5    __all__ = pyro_mysql.__all__
def init(worker_threads: int | None = 1, thread_name: str | None = None) -> None:

Initialize the Tokio runtime for async operations. This function can be called multiple times until Any async operation is called.

Arguments:
  • worker_threads: Number of worker threads for the Tokio runtime. If None, set to the number of CPUs.
  • thread_name: Name prefix for worker threads.
class Row:

A row returned from a MySQL query. to_tuple() / to_dict() copies the data, and should not be called many times.

def to_tuple(self) -> tuple[Value, ...]:

Convert the row to a Python list.

def to_dict(self) -> dict[str, Value]:

The type of the None singleton.

class IsolationLevel:

Transaction isolation level enum.

def as_str(self) -> str:

Return the isolation level as a string.

ReadUncommitted: 'IsolationLevel' = IsolationLevel.ReadUncommitted
ReadCommitted: 'IsolationLevel' = IsolationLevel.ReadCommitted
RepeatableRead: 'IsolationLevel' = IsolationLevel.RepeatableRead
Serializable: 'IsolationLevel' = IsolationLevel.Serializable
class CapabilityFlags:

MySQL capability flags for client connections.

CLIENT_LONG_PASSWORD: int = 1
CLIENT_FOUND_ROWS: int = 2
CLIENT_LONG_FLAG: int = 4
CLIENT_CONNECT_WITH_DB: int = 8
CLIENT_NO_SCHEMA: int = 16
CLIENT_COMPRESS: int = 32
CLIENT_ODBC: int = 64
CLIENT_LOCAL_FILES: int = 128
CLIENT_IGNORE_SPACE: int = 256
CLIENT_PROTOCOL_41: int = 512
CLIENT_INTERACTIVE: int = 1024
CLIENT_SSL: int = 2048
CLIENT_IGNORE_SIGPIPE: int = 4096
CLIENT_TRANSACTIONS: int = 8192
CLIENT_RESERVED: int = 16384
CLIENT_SECURE_CONNECTION: int = 32768
CLIENT_MULTI_STATEMENTS: int = 65536
CLIENT_MULTI_RESULTS: int = 131072
CLIENT_PS_MULTI_RESULTS: int = 262144
CLIENT_PLUGIN_AUTH: int = 524288
CLIENT_CONNECT_ATTRS: int = 1048576
CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA: int = 2097152
CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS: int = 4194304
CLIENT_SESSION_TRACK: int = 8388608
CLIENT_DEPRECATE_EOF: int = 16777216
CLIENT_OPTIONAL_RESULTSET_METADATA: int = 33554432
CLIENT_ZSTD_COMPRESSION_ALGORITHM: int = 67108864
CLIENT_QUERY_ATTRIBUTES: int = 134217728
MULTI_FACTOR_AUTHENTICATION: int = 268435456
CLIENT_PROGRESS_OBSOLETE: int = 536870912
CLIENT_SSL_VERIFY_SERVER_CERT: int = 1073741824
CLIENT_REMEMBER_OPTIONS: int = 2147483648
class PyroFuture:

A wrapper around a Python future that cancels the associated Rust future when dropped.

def cancel(self, /):

The type of the None singleton.

def get_loop(self, /):

The type of the None singleton.

class AsyncPool:
def get_conn(self, /):

The type of the None singleton.

def acquire(self, /):

The type of the None singleton.

def close(self, /):

The type of the None singleton.

class AsyncConn:

Concurrency

The API is thread-safe. The underlying implementation is protected by RwLock. Conn.exec_*() receives &mut self, so there is at most one statement being executed at any point.

def new(url_or_opts):

The type of the None singleton.

def start_transaction( self, /, consistent_snapshot=False, isolation_level=None, readonly=None):

The type of the None singleton.

def id(self, /):

The type of the None singleton.

def affected_rows(self, /):

The type of the None singleton.

def last_insert_id(self, /):

The type of the None singleton.

def ping(self, /):

The type of the None singleton.

def query(self, /, query):

The type of the None singleton.

def query_first(self, /, query):

The type of the None singleton.

def query_drop(self, /, query):

The type of the None singleton.

def exec(self, /, query, params=Ellipsis):

The type of the None singleton.

def exec_first(self, /, query, params=Ellipsis):

The type of the None singleton.

def exec_drop(self, /, query, params=Ellipsis):

The type of the None singleton.

def exec_batch(self, /, query, params=Ellipsis):

The type of the None singleton.

def close(self, /):

The type of the None singleton.

def reset(self, /):

The type of the None singleton.

def server_version(self, /):

The type of the None singleton.

class AsyncTransaction:
def commit(self, /):

The type of the None singleton.

def rollback(self, /):

The type of the None singleton.

def affected_rows(self, /):

The type of the None singleton.

def close_prepared_statement(self, /, _stmt):

The type of the None singleton.

def ping(self, /):

The type of the None singleton.

def query(self, /, query):

The type of the None singleton.

def query_first(self, /, query):

The type of the None singleton.

def query_drop(self, /, query):

The type of the None singleton.

def exec(self, /, query, params=Ellipsis):

The type of the None singleton.

def exec_first(self, /, query, params=Ellipsis):

The type of the None singleton.

def exec_drop(self, /, query, params=Ellipsis):

The type of the None singleton.

def exec_batch(self, /, query, params=Ellipsis):

The type of the None singleton.

class AsyncOpts:
def pool_opts(self, /, pool_opts):

The type of the None singleton.

class AsyncOptsBuilder:
def from_opts(opts):

The type of the None singleton.

def from_url(url):

The type of the None singleton.

def ip_or_hostname(self, /, hostname):

The type of the None singleton.

def tcp_port(self, /, port):

The type of the None singleton.

def socket(self, /, path):

The type of the None singleton.

def user(self, /, username):

The type of the None singleton.

def password(self, /, password):

The type of the None singleton.

def db_name(self, /, database):

The type of the None singleton.

def secure_auth(self, /, enable):

The type of the None singleton.

def wait_timeout(self, /, seconds):

The type of the None singleton.

def stmt_cache_size(self, /, size):

The type of the None singleton.

def tcp_nodelay(self, /, enable):

The type of the None singleton.

def tcp_keepalive(self, /, keepalive_ms):

The type of the None singleton.

def max_allowed_packet(self, /, max_allowed_packet):

The type of the None singleton.

def prefer_socket(self, /, prefer_socket):

The type of the None singleton.

def init(self, /, commands):

The type of the None singleton.

def compression(self, /, level):

The type of the None singleton.

def ssl_opts(self, /, _opts):

The type of the None singleton.

def local_infile_handler(self, /, _handler):

The type of the None singleton.

def pool_opts(self, /, opts):

The type of the None singleton.

def enable_cleartext_plugin(self, /, enable):

The type of the None singleton.

def client_found_rows(self, /, enable):

The type of the None singleton.

def conn_ttl(self, /, ttl_seconds):

The type of the None singleton.

def setup(self, /, commands):

The type of the None singleton.

def build(self, /):

The type of the None singleton.

class AsyncPoolOpts:
def with_constraints(self, /, constraints):

The type of the None singleton.

def with_inactive_connection_ttl(self, /, ttl):

The type of the None singleton.

def with_ttl_check_interval(self, /, interval):

The type of the None singleton.

class SyncConn:
def start_transaction( self, /, consistent_snapshot=False, isolation_level=None, readonly=None):

The type of the None singleton.

def id(self, /):

The type of the None singleton.

def affected_rows(self, /):

The type of the None singleton.

def last_insert_id(self, /):

The type of the None singleton.

def ping(self, /):

The type of the None singleton.

def query(self, /, query):

The type of the None singleton.

def query_first(self, /, query):

The type of the None singleton.

def query_drop(self, /, query):

The type of the None singleton.

def query_iter(self, /, query):

The type of the None singleton.

def exec(self, /, query, params=Ellipsis):

The type of the None singleton.

def exec_first(self, /, query, params=Ellipsis):

The type of the None singleton.

def exec_drop(self, /, query, params=Ellipsis):

The type of the None singleton.

def exec_batch(self, /, query, params_list=Ellipsis):

The type of the None singleton.

def exec_iter(self, /, query, params=Ellipsis):

The type of the None singleton.

def close(self, /):

The type of the None singleton.

def reset(self, /):

The type of the None singleton.

def server_version(self, /):

The type of the None singleton.

class SyncPool:
def get_conn(self, /):

The type of the None singleton.

def acquire(self, /):

The type of the None singleton.

class SyncPooledConn:
def start_transaction( self, /, consistent_snapshot=False, isolation_level=None, readonly=None):

The type of the None singleton.

def affected_rows(self, /):

The type of the None singleton.

def ping(self, /):

The type of the None singleton.

def query(self, /, query):

The type of the None singleton.

def query_first(self, /, query):

The type of the None singleton.

def query_drop(self, /, query):

The type of the None singleton.

def query_iter(self, /, query):

The type of the None singleton.

def exec(self, /, query, params=Ellipsis):

The type of the None singleton.

def exec_first(self, /, query, params=Ellipsis):

The type of the None singleton.

def exec_drop(self, /, query, params=Ellipsis):

The type of the None singleton.

def exec_batch(self, /, query, params_list=Ellipsis):

The type of the None singleton.

def exec_iter(self, /, query, params=Ellipsis):

The type of the None singleton.

def close(self, /):

The type of the None singleton.

class SyncPoolOpts:
def with_constraints(self, /, constraints):

The type of the None singleton.

class SyncTransaction:
def commit(self, /):

The type of the None singleton.

def rollback(self, /):

The type of the None singleton.

def affected_rows(self, /):

The type of the None singleton.

def query(self, /, query):

The type of the None singleton.

def query_first(self, /, query):

The type of the None singleton.

def query_drop(self, /, query):

The type of the None singleton.

def query_iter(self, /, query):

The type of the None singleton.

def exec(self, /, query, params=Ellipsis):

The type of the None singleton.

def exec_first(self, /, query, params=Ellipsis):

The type of the None singleton.

def exec_drop(self, /, query, params=Ellipsis):

The type of the None singleton.

def exec_batch(self, /, query, params_list=Ellipsis):

The type of the None singleton.

def exec_iter(self, /, query, params=Ellipsis):

The type of the None singleton.

class SyncOpts:
def pool_opts(self, /, pool_opts):

The type of the None singleton.

class SyncOptsBuilder:
def from_opts(opts):

The type of the None singleton.

def from_url(url):

The type of the None singleton.

def from_hash_map(self, /, params):

The type of the None singleton.

def ip_or_hostname(self, /, hostname):

The type of the None singleton.

def tcp_port(self, /, port):

The type of the None singleton.

def socket(self, /, path):

The type of the None singleton.

def bind_address(self, /, address):

The type of the None singleton.

def user(self, /, username):

The type of the None singleton.

def password(self, /, password):

The type of the None singleton.

def db_name(self, /, database):

The type of the None singleton.

def secure_auth(self, /, enable):

The type of the None singleton.

def read_timeout(self, /, seconds):

The type of the None singleton.

def write_timeout(self, /, seconds):

The type of the None singleton.

def tcp_connect_timeout(self, /, seconds):

The type of the None singleton.

def stmt_cache_size(self, /, size):

The type of the None singleton.

def tcp_nodelay(self, /, enable):

The type of the None singleton.

def tcp_keepalive_time_ms(self, /, time_ms):

The type of the None singleton.

def tcp_keepalive_probe_interval_secs(self, /, interval_secs):

The type of the None singleton.

def tcp_keepalive_probe_count(self, /, count):

The type of the None singleton.

def tcp_user_timeout_ms(self, /, timeout_ms):

The type of the None singleton.

def max_allowed_packet(self, /, max_allowed_packet):

The type of the None singleton.

def prefer_socket(self, /, prefer_socket):

The type of the None singleton.

def init(self, /, commands):

The type of the None singleton.

def connect_attrs(self, /, attrs):

The type of the None singleton.

def compress(self, /, level):

The type of the None singleton.

def ssl_opts(self, /, _opts):

The type of the None singleton.

def local_infile_handler(self, /, _handler):

The type of the None singleton.

def pool_opts(self, /, opts):

The type of the None singleton.

def additional_capabilities(self, /, capabilities):

The type of the None singleton.

def enable_cleartext_plugin(self, /, enable):

The type of the None singleton.

def build(self, /):

The type of the None singleton.

Value: 'type[None | bool | int | float | str | bytes | bytearray | tuple[Any, ...] | list[Any] | dict[str, Any] | datetime.datetime | datetime.date | datetime.time | datetime.timedelta | time.struct_time | decimal.Decimal]'

Type alias for the purpose of documenation.

These Python types can be converted to MySQL values:

  • None
  • bool
  • int
  • float
  • str
  • bytes
  • bytearray
  • tuple[Any, ...]
  • list[Any]
  • dict[str, Any]
  • datetime.datetime
  • datetime.date
  • datetime.time
  • datetime.timedelta
  • time.struct_time
  • decimal.Decimal
Params: 'type[None | tuple[Value, ...] | list[Value] | dict[str, Value]]'

Type alias for the purpose of documenation.

Parameters that can be passed to query execution methods:

  • None: No parameters
  • tuple[Value, ...]: Positional parameters for queries with ? placeholders
  • list[Value]: List of parameters for queries with ? placeholders
  • dict[str, Value]: Named parameters for queries with named placeholders

Examples:

No parameters:

await conn.exec("SELECT * FROM users")

Positional parameters:

await conn.exec("SELECT * FROM users WHERE id = ?", (123,))

Multiple positional parameters:

await conn.exec("SELECT * FROM users WHERE age > ? AND city = ?", (18, "NYC"))

Named parameters:

await conn.exec("SELECT * FROM users WHERE age > :age AND city = :city", dict(age=18, name="NYC"))