0.6.0aonly peewee 3.5+ is supported
pip install peewee-async==0.5.12
0.6.0a is published as pre-release, mind the "a" in version identifier. That means in order to install it you should specify
--pre flag for
The complete documentation:
pip for PostgreSQL:
pip install --pre peewee-async; pip install aiopg
or for MySQL:
pip install --pre peewee-async; pip install aiomysql
Create 'test' PostgreSQL database for running this snippet:
createdb -E utf-8 test
The code below is using new Python 3.5
await syntax, but older
yield from will also work!
import asyncio import peewee import peewee_async # Nothing special, just define model and database: database = peewee_async.PostgresqlDatabase( database='db_name', user='user', host='127.0.0.1', port='5432', password='password' ) 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!
You are welcome to add discussion topics or bug reports to tracker on GitHub: https://github.com/05bit/peewee-async/issues
Copyright (c) 2014, Alexey Kinev firstname.lastname@example.org
Licensed under The MIT License (MIT), see LICENSE file for more details.