Questions tagged [asyncpg]

asyncpg is an asynchronous PostgrSQL client library for Python.

203 questions
36
votes
4 answers

ObjectNotExecutableError when executing any SQL query using AsyncEngine

I'm using async_engine. When I try to execute anything: async with self.async_engine.connect() as con: query = "SELECT id, name FROM item LIMIT 50;" result = await con.execute(f"{query}") I'm getting: Exception has occurred:…
Karol Zlot
  • 2,887
  • 2
  • 20
  • 37
26
votes
3 answers

How to do persistent database connection in FastAPI?

I am writing my first project in FastAPI and I am struggling a bit. In particular, I am not sure how I am supposed to use asyncpg connection pool in my app. Currently what I have goes like this in db.py I have pgpool = None async def get_pool(): …
Mad Wombat
  • 14,490
  • 14
  • 73
  • 109
20
votes
3 answers

Best way to insert multiple rows with asyncpg

I want to insert multiple rows and get IDs back with asyncpg, i found two ways: 1: generate sql like this INSERT INTO films (code, title, did, date_prod, kind) VALUES ('B6717', 'Tampopo', 110, '1985-02-10', 'Comedy'), ('HG120', 'The Dinner…
0sv4
  • 302
  • 1
  • 2
  • 9
18
votes
2 answers

asyncpg - cannot perform operation: another operation is in progress

I am attempting to resolve the following error: asyncpg.exceptions._base.InterfaceError: cannot perform operation: another operation is in progress Here is the full traceback: Traceback (most recent call last): File "", line 1, in…
jclark754
  • 914
  • 2
  • 12
  • 30
11
votes
2 answers

Connection was closed in the middle of operation when accesing database using Python

I set up a postgresql server using Heroku in addition to my python bot which also runs on heroku but the bot fails to connect to the database I secured that the password username etc. where correct. This is the method used to connect: async def…
Akira
  • 111
  • 1
  • 3
10
votes
3 answers

how to access relationships with async sqlalchemy?

import asyncio from sqlalchemy import Column from sqlalchemy import DateTime from sqlalchemy import ForeignKey from sqlalchemy import func from sqlalchemy import Integer from sqlalchemy import String from sqlalchemy.ext.asyncio import…
muon
  • 12,821
  • 11
  • 69
  • 88
10
votes
1 answer

How can I have a synchronous facade over asyncpg APIs with Python asyncio?

Imagine an asynchronous aiohttp web application that is supported by a Postgresql database connected via asyncpg and does no other I/O. How can I have a middle-layer hosting the application logic, that is not async? (I know I can simply make…
fpbhb
  • 1,469
  • 10
  • 22
10
votes
2 answers

Asyncpg and AWS Lambda

I'm trying to use asyncpg with AWS Lambda and got next error when trying to import asyncpg Unable to import module 'handler': No module named asyncpg.protocol.protocol' I import python dependencies like at this answer with and without virtual…
chinskiy
  • 2,557
  • 4
  • 21
  • 40
9
votes
1 answer

How to apply transaction logic in FastAPI RealWorld example app?

I am using nsidnev/fastapi-realworld-example-app. I need to apply transaction logic to this project. In one API, I am calling a lot of methods from repositories and doing updating, inserting and deleting operations in many tables. If there is an…
Mamed
  • 95
  • 4
  • 16
7
votes
2 answers

cannot perform operation: another operation is in progress in pytest

I want to test some function, that work with asyncpg. If I run one test at a time, it works fine. But if I run several tests at a time, all tests except the first one crash with the error asyncpg.exceptions._base.InterfaceError: cannot perform…
Prosto_Oleg
  • 322
  • 3
  • 13
7
votes
0 answers

Async SqlAlchemy session after saving takes too much time to return

I'm moving from SQLAlchemy to SQLAlchemy[async] using postgresql as my DB. I'm following SQL Alchemy documentation https://docs.sqlalchemy.org/en/14/_modules/examples/asyncio/async_orm.html After making all changes on the code my tests got extremely…
Chubutin
  • 165
  • 2
  • 9
7
votes
2 answers

Async SqlAlchemy with FastAPI: Getting single session for all requests

I have recently migrated a REST API coded with FastApi to the new SQLAlchemy 1.4+ Async Version. My app compiles correctly and the database seems to setup just fine. The problem appears when I try to execute multiple requests, an error appears that…
Manu Sisko
  • 269
  • 2
  • 17
6
votes
2 answers

Async SQLalchemy: accessing eagerly-loaded empty relationship triggers new lazy-load, raising error

I am using sqlalchemy + asyncpg, and 'selectin' eager loading. I have Person items that have one-to-many relationships with Friends. I insert a Person into my database, with no related Friend entries. If in the same session I try and get that Person…
theo-brown
  • 653
  • 10
  • 27
6
votes
1 answer

RuntimeWarning: coroutine was never awaited. How to async / await a callback

I have a class that serves web sockets, and listen to PostgreSQL. Using asyncpg, when I try to uses add_listener, i get the error: RuntimeWarning: coroutine was never awaited. How to async / await a callback. I tried adding "await self.listener" but…
Ruben
  • 1,065
  • 5
  • 18
  • 44
5
votes
1 answer

How to handle asyncpg.exceptions.TooManyConnectionsError in Sqlalchemy

I'm developing a project with async SQLAlchemy connected through asyncpg to PostgreSQL database. The problem is this: when I make too many connections to database it raises the following exceptions: asyncpg.exceptions.TooManyConnectionsError: sorry,…
voilalex
  • 2,041
  • 2
  • 13
  • 18
1
2 3
13 14