Django Celery asynchronous task queue

 https://github.com/celery/celery

Celery's GitHub source code file

Celery is an asynchronous task queue that requires a python environment. It can generally be used for python web development frameworks, such as Django.

 

Scenes

        For example, when you develop your own website, you often need to refer to a third-party package when writing the part of sending SMS verification code. At the same time, this code for sending SMS will take a long time (several seconds, even more than ten seconds), click "Send SMS verification Code" does not start the countdown immediately, but waits for several seconds before it starts, which makes users feel very stuck. When we experience Tencent Cloud and Alibaba Cloud, the countdown often starts with one click, although the SMS may be delayed. Wait for a while, but the interface responds immediately.      

Here is a simple logic:

def get(self, request, mobile):    # 1.创建连接Redis对象    ... get_redis_connection ...        # 2. 随机生成短信验证码    ... ...        # 3.把验证码存入Redis缓存    ... setex() ...    ... setex() ...            # 4.通过第三方SDK发送短信    XXX().send_ ...  # 这一步往往十分耗时(好几秒、甚至十几秒)        # 5.最后响应回前端页面    return Response(...)

    That is to say, at the end of the Response execution, there is a send step that is too slow. At this time, we can use celery to solve this blocking problem. (Return the result after execution, no need to wait during this period, improve the throughput and response time of the system)

Configuration example

    This section takes the Django project as an example:

Create the py directory celery_tasks Under the first-level directory of the back-end project
Create the py file of main in this directory (file started as celery service)
Create the py file of config in this directory (as a file storing configuration)

This is the most basic file directory situation of celery. After the directory structure is created, install celery (pip install celery or import directly from the compiler).

    Create a package in the celery_tasks directory, and create a package for any task you want.

Create the py directory of xxx here, and then create the py file of tasks     under it [note that the name is fixed]

       For example, to configure sending text messages, then in main.py, you need to ① configure the celery object, ② load the config configuration file, ③ register asynchronous tasks, and configure the Redis server location in config.py. It is relatively easy to use.

Guess you like

Origin blog.csdn.net/lxd_max/article/details/127895345