| 1 |
aiopg |
| 2 |
===== |
| 3 |
.. image:: https://github.com/aio-libs/aiopg/workflows/CI/badge.svg |
| 4 |
:target: https://github.com/aio-libs/aiopg/actions?query=workflow%3ACI |
| 5 |
.. image:: https://codecov.io/gh/aio-libs/aiopg/branch/master/graph/badge.svg |
| 6 |
:target: https://codecov.io/gh/aio-libs/aiopg |
| 7 |
.. image:: https://badges.gitter.im/Join%20Chat.svg |
| 8 |
:target: https://gitter.im/aio-libs/Lobby |
| 9 |
:alt: Chat on Gitter |
| 10 |
|
| 11 |
**aiopg** is a library for accessing a PostgreSQL_ database |
| 12 |
from the asyncio_ (PEP-3156/tulip) framework. It wraps |
| 13 |
asynchronous features of the Psycopg database driver. |
| 14 |
|
| 15 |
Example |
| 16 |
------- |
| 17 |
|
| 18 |
.. code:: python |
| 19 |
|
| 20 |
import asyncio |
| 21 |
import aiopg |
| 22 |
|
| 23 |
dsn = 'dbname=aiopg user=aiopg password=passwd host=127.0.0.1' |
| 24 |
|
| 25 |
async def go(): |
| 26 |
pool = await aiopg.create_pool(dsn) |
| 27 |
async with pool.acquire() as conn: |
| 28 |
async with conn.cursor() as cur: |
| 29 |
await cur.execute("SELECT 1") |
| 30 |
ret = [] |
| 31 |
async for row in cur: |
| 32 |
ret.append(row) |
| 33 |
assert ret == [(1,)] |
| 34 |
|
| 35 |
loop = asyncio.get_event_loop() |
| 36 |
loop.run_until_complete(go()) |
| 37 |
|
| 38 |
|
| 39 |
Example of SQLAlchemy optional integration |
| 40 |
------------------------------------------ |
| 41 |
|
| 42 |
.. code:: python |
| 43 |
|
| 44 |
import asyncio |
| 45 |
from aiopg.sa import create_engine |
| 46 |
import sqlalchemy as sa |
| 47 |
|
| 48 |
metadata = sa.MetaData() |
| 49 |
|
| 50 |
tbl = sa.Table('tbl', metadata, |
| 51 |
sa.Column('id', sa.Integer, primary_key=True), |
| 52 |
sa.Column('val', sa.String(255))) |
| 53 |
|
| 54 |
async def create_table(engine): |
| 55 |
async with engine.acquire() as conn: |
| 56 |
await conn.execute('DROP TABLE IF EXISTS tbl') |
| 57 |
await conn.execute('''CREATE TABLE tbl ( |
| 58 |
id serial PRIMARY KEY, |
| 59 |
val varchar(255))''') |
| 60 |
|
| 61 |
async def go(): |
| 62 |
async with create_engine(user='aiopg', |
| 63 |
database='aiopg', |
| 64 |
host='127.0.0.1', |
| 65 |
password='passwd') as engine: |
| 66 |
|
| 67 |
async with engine.acquire() as conn: |
| 68 |
await conn.execute(tbl.insert().values(val='abc')) |
| 69 |
|
| 70 |
async for row in conn.execute(tbl.select()): |
| 71 |
print(row.id, row.val) |
| 72 |
|
| 73 |
loop = asyncio.get_event_loop() |
| 74 |
loop.run_until_complete(go()) |
| 75 |
|
| 76 |
.. _PostgreSQL: http://www.postgresql.org/ |
| 77 |
.. _asyncio: https://docs.python.org/3/library/asyncio.html |
| 78 |
|
| 79 |
Please use:: |
| 80 |
|
| 81 |
$ make test |
| 82 |
|
| 83 |
for executing the project's unittests. |
| 84 |
See https://aiopg.readthedocs.io/en/stable/contributing.html for details |
| 85 |
on how to set up your environment to run the tests. |