Problem with mail flood when connecting to database
<p>When I'm connecting to a database I'm sending mails on connection errors. The problem is that Python tries so often to connect to the database that I'm getting flooded by mails (hundreds per minute) in the case of errors.
To clarify:</p>

<pre><code>try:
    #connect to db
except psycopg2.Error, msg:
    # write error mail
</code></pre>

<p>Now I want that python only sends one mail per 'error session', so I tried:</p>

<pre><code>mail_sent = False
try:
    #connect to db
    mail_sent = False
except [..]:
    if not mail_sent:
        #send mail
        sent_mail = True
</code></pre>

<p>But this doesn't work because the code is in an infinite loop (SocketServer) and so <code>sent_mail</code> is set to <code>False</code> on every cycle.</p>

<p>Has anyone an idea how to solve this problem?</p>
