开发者

Problem with mail flood when connecting to database

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:

try:
    #connect to db
except psycopg2.Error, msg:
    # write error mail

Now I want that开发者_如何转开发 python only sends one mail per 'error session', so I tried:

mail_sent = False
try:
    #connect to db
    mail_sent = False
except [..]:
    if not mail_sent:
        #send mail
        sent_mail = True

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

Has anyone an idea how to solve this problem?


Use a file lock to make sure only one session of you test runs, and add a sleep() to make sure you only test every, say, 5 minutes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜