Skip to main content

Async support for SQLAlchemy.

Project description

PyPI Version Documentation Travis Coverage MIT License

sqlalchemy_aio adds asyncio and Trio support to SQLAlchemy core, derived from alchimia.

Getting started

import asyncio

from sqlalchemy_aio import ASYNCIO_STRATEGY

from sqlalchemy import (
    Column, Integer, MetaData, Table, Text, create_engine, select)
from sqlalchemy.schema import CreateTable, DropTable


async def main():
    engine = create_engine(
        # In-memory sqlite database cannot be accessed from different
        # threads, use file.
        'sqlite:///test.db', strategy=ASYNCIO_STRATEGY
    )

    metadata = MetaData()
    users = Table(
        'users', metadata,
        Column('id', Integer, primary_key=True),
        Column('name', Text),
    )

    # Create the table
    await engine.execute(CreateTable(users))

    conn = await engine.connect()

    # Insert some users
    await conn.execute(users.insert().values(name='Jeremy Goodwin'))
    await conn.execute(users.insert().values(name='Natalie Hurley'))
    await conn.execute(users.insert().values(name='Dan Rydell'))
    await conn.execute(users.insert().values(name='Casey McCall'))
    await conn.execute(users.insert().values(name='Dana Whitaker'))

    result = await conn.execute(users.select(users.c.name.startswith('D')))
    d_users = await result.fetchall()

    await conn.close()

    # Print out the users
    for user in d_users:
        print('Username: %s' % user[users.c.name])

    # Supports context async managers
    async with engine.connect() as conn:
        async with conn.begin() as trans:
            assert await conn.scalar(select([1])) == 1

    await engine.execute(DropTable(users))


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

Getting started with Trio

To use the above example with Trio, just change the following:

import trio
from sqlalchemy_aio import TRIO_STRATEGY

async def main():
    engine = create_engine('sqlite:///test.db', strategy=TRIO_STRATEGY)

    ...

trio.run(main)

What is this?

It’s not an asyncio implementation of SQLAlchemy or the drivers it uses. sqlalchemy_aio lets you use SQLAlchemy by running operations in a separate thread.

If you’re already using run_in_executor to execute SQLAlchemy tasks, sqlalchemy_aio will work well with similar performance. If performance is critical, perhaps asyncpg can help.

Documentation

The documentation has more information, including limitations of the API.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlalchemy_aio-0.14.1.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

sqlalchemy_aio-0.14.1-py2.py3-none-any.whl (24.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file sqlalchemy_aio-0.14.1.tar.gz.

File metadata

  • Download URL: sqlalchemy_aio-0.14.1.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.2

File hashes

Hashes for sqlalchemy_aio-0.14.1.tar.gz
Algorithm Hash digest
SHA256 baaed3643745683b75dafaeeabf7d6c51861713c551e729906b53b9ee266f54d
MD5 644ae755cc314497499167db8bbf9333
BLAKE2b-256 356bd3522f584cce6452c5930d9ec02bb30c3cb5a77a0143c40f950f0834c414

See more details on using hashes here.

File details

Details for the file sqlalchemy_aio-0.14.1-py2.py3-none-any.whl.

File metadata

  • Download URL: sqlalchemy_aio-0.14.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 24.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.2

File hashes

Hashes for sqlalchemy_aio-0.14.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 bdf8ee3ca5c30bcc794d9445fa4eb69e9a86f57d9bfd6d2a4ecdb9703d8e2b86
MD5 9396a7726608e41ab17cb7d40ddfdc48
BLAKE2b-256 eb88ac248d784cbb93881ebadc1ab2e858689932a45f7c85f80d1581d9882f8e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page