# Asynchronous psycopg2
psycopg2 connections are thread-safe, which means that many threads can access the same database, either:
- Every thread creating its own
- Every thread using the same connection but each creating different
I'd generally recommend, if possible, to have separate connections per thread as that will mean that SQL queries will be executed in different sessions.
# asyncio support in psycopg2
psycopg2 does not support
await. However, there is another library built on top of psycopg2 that does: aiopg.
aiopg you can use the
await keywords, and you'll gain the ability to multi-task while waiting for database operations.
If you don't know how
asyncio or the
await keywords work, I'd recommend a section in our Complete Python Course (opens new window) which covers this extensively. This talk (opens new window) by David Beazley is also excellent.