According to psycopg2
docs:
libpq
connections shouldn’t be used by a forked processes, so when using a module such asmultiprocessing
or a forking web deploy method such as FastCGI make sure to create the connections after the fork.
Following the link from that document leads to:
On Unix, forking a process with open
libpq
connections can lead to unpredictable results because the parent and child processes share the same sockets and operating system resources. For this reason, such usage is not recommended, though doing an exec from the child process to load a new executable is safe.
But it seems there's no inherent problem with forking processes with open sockets. So what's the reason for psycopg2
's warning against forking when connections are open?
The reason for my question is that I saw a (presumably successful) multiprocessing approach that opened a connection right before forking.
Perhaps it is safe to fork open connections under some restrictions (e.g., only one process actually ever uses the connection, etc.)?