Skip to main content

Peewee support for async frameworks (Asyncio, Trio)

Project description

aio-peewee – Peewee support for async frameworks (Asyncio, Trio, Curio)

Tests Status PYPI Version Python Versions

The library doesn’t make peewee work async, but allows you to use Peewee with your asyncio based libraries correctly.

Features

  • Tasks Safety. The library tracks of the connection state using Task-local storage, making the Peewee Database object safe to use with multiple tasks inside a loop.

  • Async management of connections for Peewee Connections Pool

Requirements

  • python >= 3.8

Installation

aio-peewee should be installed using pip:

pip install aio-peewee

QuickStart

from aiopeewee import db_url

db = db_url.connect('postgres+async://locahost:5432/database')

async def main(id=1):
    async with db:
        item = Model.get(Model.id == 1)

    return item.name

Usage

Initialization

from aiopeewee import PostgresqlDatabaseAsync, SqliteDatabaseAsync, MySQLDatabaseAsync, CockroachDatabaseAsync

 db = PostgresqlDatabaseAsync('my_app', user='app', password='db_password', host='10.1.0.8', port=3306)

Async Connect

# Manual
async def main():
     await db.connect_async()
     # ...
     await db.close_async()

 # Context manager
async def main():
     async with db:
         # ...

Connection Pooling

from aiopeewee import PooledPostgresqlDatabaseAsync, PooledSqliteDatabaseAsync, PooledMySQLDatabaseAsync, PooledCockroachDatabaseAsync

db = PooledPostgresqlDatabaseAsync('my_database', max_connections=8, stale_timeout=300, user='postgres')

Database URL

from aiopeewee import db_url

 db0 = db_url.connect('cockroachdb+async://localhost/db', **db_params)
 db1 = db_url.connect('cockroachdb+pool+async://localhost/db', **db_params)
 db2 = db_url.connect('mysql+async://localhost/db', **db_params)
 db3 = db_url.connect('mysql+pool+async://localhost/db', **db_params)
 db4 = db_url.connect('postgres+async://localhost/db', **db_params)
 db5 = db_url.connect('postgres+pool+async://localhost/db', **db_params)
 db6 = db_url.connect('sqlite+async://localhost/db', **db_params)
 db7 = db_url.connect('sqlite+pool+async://localhost/db', **db_params)
 db8 = db_url.connect('sqliteexc+async://localhost/db', **db_params)
 db9 = db_url.connect('sqliteexc+pool+async://localhost/db', **db_params)

ASGI Middleware

import datetime as dt

from asgi_tools import App
from aiopeewee import PeeweeASGIPlugin
import peewee as pw


db = PeeweeASGIPlugin(url='sqlite+async:///db.sqlite')


@db.register
class Visit(pw.Model):
    created = pw.DateTimeField(default=dt.datetime.utcnow())
    address = pw.CharField()


db.create_tables()


app = App()


@app.route('/')
async def visits_json(request):
    """Store the visit and load latest 10 visits."""
    Visit.create(address=request.client[0])
    return [{
        'id': v.id, 'address': v.address, 'timestamp': round(v.created.timestamp()),
    } for v in Visit.select().order_by(Visit.id.desc()).limit(10)]


app = db.middleware(app)

Curio

aio-peewee uses contextvars to store db connections. So you have to enable contextvars for Curio: https://curio.readthedocs.io/en/latest/howto.html#how-do-you-use-contextvars

Bug tracker

If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/aio-peewee/issues

Contributing

Development of the project happens at: https://github.com/klen/aio-peewee

License

Licensed under a MIT license.

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

aio-peewee-0.4.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

aio_peewee-0.4.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file aio-peewee-0.4.0.tar.gz.

File metadata

  • Download URL: aio-peewee-0.4.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for aio-peewee-0.4.0.tar.gz
Algorithm Hash digest
SHA256 26618b090347afd02c7213f24c12128eb5224ca5641509f38c3e77ad5af39086
MD5 16ab2656d24ba41883bcded23884ec66
BLAKE2b-256 150db59308939f13d6469126c41070f75f7ffc10534faa000f26b8554715dfb0

See more details on using hashes here.

File details

Details for the file aio_peewee-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: aio_peewee-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for aio_peewee-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2aaab531b7abe8d227a7498a28ae2f36affe9316c6152b3b0a8330c79e228e81
MD5 b7b727bd6913eab820a201406f1cebef
BLAKE2b-256 6aa97d19cf123b8fcd035495a6744a1cae0ca12530603088ddbcd956db32bb77

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