The library uses
psycopg2 connections in asynchronous mode
Literally it is an (almost) transparent wrapper for psycopg2 connection and cursor, but with only exception.
You should use
yield from conn.f() instead of just call
Properties are unchanged, so
conn.prop is correct as well as
conn.prop = val.
import asyncio import aiopg dsn = 'dbname=aiopg user=aiopg password=passwd host=127.0.0.1' async def go(): async with aiopg.create_pool(dsn) as pool: async with pool.acquire() as conn: async with conn.cursor() as cur: await cur.execute("SELECT 1") ret =  async for row in cur: ret.append(row) assert ret == [(1,)] loop = asyncio.get_event_loop() loop.run_until_complete(go())
For documentation about connection and cursor methods/properties please go to psycopg docs: http://initd.org/psycopg/docs/
psycopg2 creates new connections with
option in asynchronous mode. Autocommitting cannot be disabled.
See Transactions about transaction usage in autocommit mode.
Throughout this documentation, examples utilize the async/await syntax introduced by PEP 492 that is only valid for Python 3.5+.
If you are using Python 3.4, please replace
yield from and
async def with a
For example, this:
async def coro(...): ret = await f()
shoud be replaced by:
@asyncio.coroutine def coro(...): ret = yield from f()
see also yield from/@coroutine style examples.
SQLAlchemy and aiopg¶
We have found it to be very annoying to write raw SQL queries manually, so we introduce support for sqlalchemy query builders:
import asyncio from aiopg.sa import create_engine import sqlalchemy as sa metadata = sa.MetaData() tbl = sa.Table('tbl', metadata, sa.Column('id', sa.Integer, primary_key=True), sa.Column('val', sa.String(255))) async def go(): async with create_engine(user='aiopg', database='aiopg', host='127.0.0.1', password='passwd') as engine: async with engine.acquire() as conn: await conn.execute(tbl.insert().values(val='abc')) async for row in conn.execute(tbl.select().where(tbl.c.val=='abc')): print(row.id, row.val) loop = asyncio.get_event_loop() loop.run_until_complete(go())
We believe constructions like
tbl.select().where(tbl.c.val=='abc') to be very handy and
pip3 install aiopg
You can use standard one from your distro like:
$ sudo apt-get install python3-psycopg2
$ sudo apt-get install libpq-dev
Also you probably want to use
pip3 install sqlalchemy
The project is hosted on GitHub
Please feel free to file an issue on bug tracker if you have found a bug or have some suggestion for library improvement.
The library uses Travis for Continious Integration.
aio-libs google group: https://groups.google.com/forum/#!forum/aio-libs
Feel free to post your questions and ideas here.