Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
Last active March 24, 2018 19:15
Show Gist options
  • Save vlad-bezden/d94ebe5a4be9fc560039cd74ee5753ed to your computer and use it in GitHub Desktop.
Save vlad-bezden/d94ebe5a4be9fc560039cd74ee5753ed to your computer and use it in GitHub Desktop.
How to use async PostgreSQL queries execution using aiopg and asynio libraries
from time import time
import aiopg
import asyncio
CONN_INFO = {
'host': 'POSTGRESQL_SERVER',
'user': 'user_name',
'port': 1234,
'database': 'some_dabase',
}
dsn = 'dbname={database} user={user} host={host}'.format(**CONN_INFO)
async def get_data(pool):
start = time()
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute('SELECT * FROM some_table limit 100000')
result = await cur.fetchall()
print(f'there are {len(result)} records. Exec time: {time() - start}')
return result
async def main():
pool = await aiopg.create_pool(dsn)
start = time()
tasks = []
for i in range(3):
tasks.append(loop.create_task(get_data(pool)))
tasks, stat = await asyncio.wait(tasks)
for task in tasks:
print(f'number of items: {len(task.result())}')
print(f'total exec time: {time() - start} secs')
print('exiting main')
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
print('exiting program')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment