I'm building a webapp using django 1.7 on python 2.7, deployed in production with apache and mod_wsgi in daemon mode. I'm having a problem that wsgi hangs whenever I have an exception logged (with python logging) and the AdminEmailHandler configured in my settings.py.
When I request the page the browser hangs until eventually the apache log shows "End of script output before headers: wsgi.py"
I have two logging handlers configured, a FileHandler and AdminEmailHandler, and when I comment out the AdminEmailHandler the problem doesn't occur.
'handlers': {
'file': {
'level': 'INFO',
'class': 'logging.FileHandler',
'filename': '/var/log/apache2/mylog',
'formatter':'simple',
},
'email': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
'formatter':'simple',
}
I have seen that I could configure wsgi with
WSGIApplicationGroup %{GLOBAL}
and that there are application issues with mod_wsgi (from this question).
However I would like to keep daemon mode if possible, and the AdminEmailHandler seems to be a core piece of the logging functionality that I'm surprised this hasn't been encountered before!
Thanks in advance for your help
UPDATE
I fixed the issue with my own subclass of AdminEmailHandler that delegates the email sending to a separate thread. Thanks to Vinay for the help!
from threading import Thread
import django.utils.log as djangolog
class MyAdminEmailHandler(djangolog.AdminEmailHandler):
def emit(self, record):
try:
thread = Thread(target=djangolog.AdminEmailHandler.emit, args=(self, record))
thread.start()
except:
self.handleError(record)