We are calling postgres using psycopg2 2.7.5  this way that we perform a query then perform some operation on data that we received and then we open new connection and perform another query and so on. 
Usually the query takes between 15 s to 10 min. 
Occasinally after 2 h we receive error: Python Exception : connection already closed
What may be the reason for that? Data is the same and query is the same and sometimes the same query gives results back in 3 min and sometimes it gets that timeout after 2 hrs. 
I wonder if it is possible that connection is broken earlier but in python we get that information for some reason after 2 hrs? 
I doubt that there are any locks on DB at the moment when we perform a query but it may be under huge load and max number of connections may be reached (not confirmed but this is an option).
What would be the best way to track down the problem? Firewall is set to 30 min timeout.
We are calling postgres using psycopg2 2.7.5 this way that we perform a query then perform some operation on data that we received and then we open new connection and perform another query and so on.

Why do you keep opening new connections?  What do you do with the old one, and when do you do it?

I wonder if it is possible that connection is broken earlier but in python we get that information for some reason after 2 hrs?

In general, a broken connection won't be detected until you try to use it.  If you are using a connection pooler, it is possible the pool manager checks up on the connection periodically in the background.
postgres/psycopg2 strange timeout after 2 hrs