Skip to content

How to correctly perform multithreading with psycopg2 ? #1652

Closed Answered by dvarrazzo
silverbullet1 asked this question in Q&A
Discussion options

You must be logged in to vote

@silverbullet1 can you please copy your question here?

The use of multiple threads of the same connection is safe, but you must understand what psycopg is doing with the Postgres session. Threads sharing the same connection are also sharing the same session, so they are all operating in the same transaction, because postgres can only create one transaction per session. You can imagine several cats taking turns to type psql command on the same keyboard: psycopg makes sure that only one cat at time can use the keyboard and that two commands don't mix up (ending up typing SELE-UPDATE-CT or taking one the result of another) but, because the connection is responsible for the session and the tr…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@silverbullet1
Comment options

@dvarrazzo
Comment options

Answer selected by silverbullet1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants