psycopg2 connection already closed
The most likely reason is you have a firewall or gateway which is not very patient and is dropping the connection after 5 minutes.  Usually when this happens, it makes it look to the client like the server closed the connection, and to the server like the client closed the connection.  That seems to be compatible with the error message you see in python.  What does the server's log file blame the problem on?