Closed GoranSustekJr closed 2 months ago
Managed to fix it by creating a new instance of connection
and cursor
for every thread job with passing it down to query function.
You can't share cursors across threads. You can share connections, but you will serialize the work. The best option is to use a connection pool so you can have some proper concurrency and no connection overhead.
Also, if you use psycopg 3, not only you get a proper connection pool, you can use async connections and not bother with threads.
This is a bug tracker
Please complete the following information:
Describe the bug I am using fastAPI as my API. In the request it starts a function from a different file by adding it to a ThreadPoolExecutor so it executes in the background:
In the a second file are the add querys:
The connection and cursor variables come from a third file:
When I send one or two requests in short span of time, it executes nicely, but when I send 3, 4+ requests an error occurs at
tid = cursor.fetchone()
. The output is:no results to fetch
but when I print the query from the cursor I get the following:b'INSERT INTO tid_auid ( tid, auid ) VALUES ( 206, NULL )'
which is a completely different query that the one executed. It seems that because of multiple instances of the same function which are being executed at the same time, one of the functions overrides other functions cursor query.