Skip to content

Latest commit

 

History

History
40 lines (34 loc) · 1.05 KB

README.md

File metadata and controls

40 lines (34 loc) · 1.05 KB

Sanic Asyncpg

Example how to use Python Sanic and Postgre Asyncpg

Prerequisites

  1. Install python sanic
    $ pip install sanic
  1. Install PostgreSQL Database Client
    $ pip install asyncpg
  1. Create conection pool
from asyncpg import create_pool

@app.listener('before_server_start')
async def register_db(app, loop):
    # Create a database connection pool
    conn = "postgres://{user}:{password}@{host}:{port}/{database}".format(
        user='postgres', password='secret', host='localhost',
        port=5432, database='some_database'
    )
    app.config['pool'] = await create_pool(
        dsn=conn,
        min_size=10, #in bytes,
        max_size=10, #in bytes,
        max_queries=50000,
        max_inactive_connection_lifetime=300,
        loop=loop)
  1. To see detail code you can check main.py
  2. Happy coding :)

Reference

  1. Sanic
  2. Asyncpg