django-simple-task在Django 3中运行后台任务,而无需其他服务和工作程序

网友投稿 984 2022-11-01

django-simple-task在Django 3中运行后台任务,而无需其他服务和工作程序

django-simple-task在Django 3中运行后台任务,而无需其他服务和工作程序

Django Simple Task

django-simple-task runs background tasks in Django 3 without requiring other services and workers. It runs them in the same event loop as your ASGI APPlication. It is not resilient as a proper task runner such as Celery, but works for some simple tasks and has less overall overheads.

Guide

Install the package:

pip install django-simple-task

Added it to installed apps:

# settings.pyINSTALLED_APPS = [ ... 'django_simple_task']

Apply ASGI middleware :

# asgi.pyfrom django_simple_task import django_simple_task_middlwareapplication = django_simple_task_middlware(application)

Call a background task in Django view:

from django_simple_task import deferdef task1(): time.sleep(1) print("task1 done")async def task2(): await asyncio.sleep(1) print("task2 done")def view(requests): defer(task1) defer(task2) return HttpResponse(b"My View")

It is required to run Django with ASGI server. Official Doc

Configurations

Concurrency level can be controlled by adding DJANGO_SIMPLE_TASK_WORKERS to settings. Defaults to 1.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:PrettyStackTrace允许Swift命令行程序在引发终止信号时打印执行行为踪迹
下一篇:107+ 编码面试问题,包括详细的解决方案,测试用例和程序分析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~