peewee-async — peewee-async 0.6.1a documentation (original) (raw)
peewee-async is a library providing asynchronous interface powered by asyncio for peewee ORM.
Current state: alpha, yet API seems fine and mostly stable.
In current version (0.5.x) new-high level API is introduced while older low-level API partially marked as deprecated.
- Works on Python 3.4+
- Has support for PostgreSQL via aiopg
- Has support for MySQL via aiomysql
- Single point for high-level async API
- Drop-in replacement for sync code, sync will remain sync
- Basic operations are supported
- Transactions support is present, yet not heavily tested
The source code is hosted on GitHub.
Quickstart¶
import asyncio import peewee import peewee_async
Nothing special, just define model and database:
database = peewee_async.PostgresqlDatabase('test')
class TestModel(peewee.Model): text = peewee.CharField()
class Meta:
database = database
Look, sync code is working!
TestModel.create_table(True) TestModel.create(text="Yo, I can do it sync!") database.close()
Create async models manager:
objects = peewee_async.Manager(database)
No need for sync anymore!
database.set_allow_sync(False)
async def handler(): await objects.create(TestModel, text="Not bad. Watch this, I'm async!") all_objects = await objects.execute(TestModel.select()) for obj in all_objects: print(obj.text)
loop = asyncio.get_event_loop() loop.run_until_complete(handler()) loop.close()
Clean up, can do it sync again:
with objects.allow_sync(): TestModel.drop_table(True)
Expected output:
Yo, I can do it sync!
Not bad. Watch this, I'm async!
Install¶
Install latest version from PyPI.
For PostgreSQL:
pip install peewee-async aiopg
For MySQL:
pip install peewee-async aiomysql
Install from sources¶
git clone https://github.com/05bit/peewee-async.git cd peewee-async python setup.py install
Running tests¶
Prepare environment for tests:
- Clone source code from GitHub as shown above
- Create PostgreSQL database for testing, i.e. named ‘test’
- Create
tests.json
config file based ontests.json.sample
Then run tests:
Report bugs and discuss¶
You are welcome to add discussion topics or bug reports to tracker on GitHub!