Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the proper way to run some Python code asynchronously?

I needed to send mail from my plain Flask app, so I thought the simplest way would be to send it using smtplib. But I had to do it asynchronously - you can't just insert a 3 second delay into the request - right? So I add the email to a queue (psql table), and send it from another program that reads this table and uses smptlib.

This second program (maildonkey) is running as a separate process, in an independent upstart service.

Now I need another one of those little asynchoronous services, and I'm thinking if I should write another python script (third, counting my Flask app and 'maildonkey') or should I use something like Python's 'multiprocess', or even 'threads' and rewrite the second program?

(When I was programming in Clojure, I could easily run code in a separate thread with 'futures', so normally I would do that.)

like image 338
Hugo Avatar asked Apr 04 '12 01:04

Hugo


1 Answers

You should consider using Celery. It is very widely used in web frameworks for asynchronous processing and supports a lot of different backends like AMQP, databases etc.

like image 171
Praveen Gollakota Avatar answered Sep 26 '22 03:09

Praveen Gollakota