Skip to content

FerdinaKusumah/sanic-asyncpg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

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

Releases

No releases published

Packages

No packages published

Languages