I installed a local SMTP server and used logging.handlers.SMTPHandler to log an exception using this code:
import logging
import logging.handlers
import time
gm = logging.handlers.SMTPHandler(("localhost", 25), 'info@somewhere.com', ['my_email@gmail.com'], 'Hello Exception!',)
gm.setLevel(logging.ERROR)
logger.addHandler(gm)
t0 = time.clock()
try:
1/0
except:
logger.exception('testest')
print time.clock()-t0
It took more than 1sec to complete, blocking the python script for this whole time. How come? How can I make it not block the script?
Here’s the implementation I’m using, which I based on this Gmail adapted SMTPHandler.
I took the part that sends to SMTP and placed it in a different thread.
Usage example: