0

In my django application I am using django-notification to send notifications. However I noticed that in some cases (when sending multiple notifications) my web application is giving delayed responses. Although I am sending notifications through Ajax requests, I still think it would be best if I could implement mailtools library which provide threaded emails.

Has anyone implemented such a thing? Is it easy? How can I use ThreadedMailer from mailtools in django-notification?

or, is there another alternative?

xpanta
  • 8,124
  • 15
  • 60
  • 104

1 Answers1

2

Use Celery for this purpose. It's easy to setup with django and you can use the code you're using right now.

The ajax request puts the email into task queue and returns. You could return your task id if you want to check later if the task succeeded.

Update:

Celery only enables you to call your functions in backgound. Say in ajax view you called:

send_email(…)

Now in tasks.py you should define function:

@task
def send_email(…)

And in the view you will call it by:

send_email.delay(…)

And that's it. The email will be sent by background worker deamon using your existing python code.

This doesn't make django-notification obsolete. Celery does completly different thing and can be used with any lib you can imagine.

The only change is task arguments have to be pickable. It means you have to pass db ids, not whole objects, etc.

Krzysztof Szularz
  • 5,151
  • 24
  • 35
  • That is nice, thanks. Does Celery make `django-notification` obsolete? Do all email actions get queued in Celery, automatically? (or they need to be in ajax requests?) – xpanta Nov 26 '12 at 08:52
  • That is amazing! One last thing. I guess in `def send_mail()` I add `notification.send(args)` and that's it? – xpanta Nov 26 '12 at 09:25
  • I believe you're right. I've never used `django-notification`, but I use Celery on every day basis to process hundreds of tasks. – Krzysztof Szularz Nov 27 '12 at 09:40