Paul Hoang
Paul Hoang

Reputation: 1074

Creating a new thread to email admin

Inside my Django code, I have something like the following:

   #ajax call 
   ...
    try:
       do sth
       ...
    except Exception as e:
       _email_admin()
       return HttpResponseServerError(e)

The above would, however, slow down the return to users, which is definitely not something cool to have.

So I'm asking if you can advise me on the possibility of forking a new thread to send email so that it won't delay the return to users?

Thank you.

PS: I know that if I don't catch the exception, a 500 error is created and Django would email the admins. But I need to catch the exception so that I can still return the Ajax call.

Upvotes: 0

Views: 136

Answers (1)

Cat Plus Plus
Cat Plus Plus

Reputation: 129764

Your code is doing the same thing Django would do, just remove it. I don't remember if mails are sent on a worker thread, but it doesn't matter — if you have to send error mail, then your code is broken. Saving few milliseconds will buy you absolutely nothing. Especially that this is async call.

Also, for stuff like this you're better of with Celery, or something like it, rather than creating threads.

Upvotes: 1

Related Questions