I think there are two different approaches here:
- Task manager (e.g. celery)
- Asynchronous implementation (e.g. gevent)
What you achieve with each of them is different. With Celery, you can run all the code needed to compute a response synchronously, and then run any other operation in the background (for example, saving to logs). So the answer should be faster.
With gevent, what you achieve is to run different instances of your handler in parallel. So, if you have one request, you will not see any difference in response time, but if you have thousands of simultaneous requests, the performance will be much better. The reason for this is that without gevent, when your code performs an I / O operation, it blocks the execution of this process, while with gevent, the CPU can continue to execute other requests while waiting for the I / O operation.
Setting up gevent is much easier than installing Celery. If you use gunicorn, you simply install gevent and change the worker type to gevent. Another advantage is that you can parallelize any operation that is required in the response (for example, retrieving the response from the database). In celery, you cannot use the result of the Celery task in your answer.
What I would recommend is to start using gevent and consider adding Celery later (and having both of them) if:
- The output of the task that you will process using Celery is not required in the answer
- You have another machine for your celery tasks, or using your server has some peaks and some downtime (if your server is 100% all the time, you will not get anything good from using Celery)
- The amount of work your celeryn tasks will perform is worth the overhead of using celery
source share