This gist goes over how asyncpg can be integrated with discord.py. In addition with the example, this document also serves as an explanation on some design decisions for who are interested.
Please contact noellieuwu
on the discord.py server if
you are interested in contributing to changes or have
feedback.
This example explictly uses connection pools, which is a way to reduce the cost of opening and closing connections by maintaining a “pool” of open connections that can be passed from database operation to database operation as needed. This spares the expense of opening and closing fresh connections for each operation that would be done to the database. In addition of being best practice, they provide several advantages, such as:
- Improved database and application performance
- Lower latency
- Cheaper way to perform database operations
These princples apply to most databases such as PostgreSQL and MongoDB, but locally file-bound databases
such as SQLite are not bound to these performance gains. Although asqlite
does provide connection pools, Danny, the author of asqlite
discussed that
the rationale is to allow transactions to work properly with the barriers of asyncio
(message can be found here).
aiosqlite
does not do this either as all connections are bound to file I/O speeds, and does not
present an speedy alternative for reducing these latencies. Thus, when using SQLite as your
database, connection pooling does not apply.
I cannot guantee that these examples are not full of biases, but I tried my best to give an rationale and non-biased presentation of how the examples would be designed. Some people may complain about an certain style of my design and would prefer to do things differently, but this serves as an basic example to guide and help others.