0

Psycopg2 database connection throughout multiple processes

I am having trouble with psycopg2. How do you have a connection throughout multiple processes? I want to initialize a connection in p1.py by user input (scoped in __name__ == '__main__', NOT a field variable), then I start a process from a function in p2.py. p1.py and p2.py both need to make queries from the database, and both have to be able to commit. Do I just make an object and just pass the connection reference throughout processes (if so would the connection stay open, or do I open it before or after I pass it? And if so how would closing the connection work?)? Or do I have to establish a connection per process?

27th Sep 2022, 10:58 PM
Odyel
Odyel - avatar
1 Antwort
0
I figured it out. Just instance a connection in each process rather than passing the connection reference as it can't be pickled anyways, so it wouldn't work between processors. Then use it just as you would normally in the process.
29th Sep 2022, 8:31 PM
Odyel
Odyel - avatar