Python error: cannot be executed while asynchronous request is executed

How to prevent the error " ProgrammingError: execute cannot be used while an asynchronous query is underway "? From the docs it says that I should use psycopg2.extras.wait_select if Im uses coroutine support like gevent., But Im still getting this error when Im using it. Ive highlighted the Im error included in the snippet below.

 con = psycopg2.connect(database=DATABASE_NAME, user=DATABASE_USERNAME) def execute_query(cur, query, params): psycopg2.extras.wait_select(con) cur.execute(query, params) psycopg2.extras.wait_select(con) rows = cur.fetchall() print rows[0] cur = con.cursor() query = "SELECT * FROM mytable" gevent.joinall([ gevent.spawn(execute_query, cur, query, None), gevent.spawn(execute_query, cur, query, None), gevent.spawn(execute_query, cur, query, None), gevent.spawn(execute_query, cur, query, None) ]) 
+6
source share
1 answer

You are trying to make several transactions simultaneously on the same connection. The psycopg documentation states that it is not thread safe and will result in an error. See below Asynchronous Support and Coroutine Library Support

One possible solution is to use one database connection, each with one cursor, per coroutine (4 different connections and cursors in this case).

+7
source

Source: https://habr.com/ru/post/985961/


All Articles