在django中的异步任务处理程序之间切换,而不需要几秒钟。
django-swappable-tasks的Python项目详细描述
Django可交换任务
简介
Considering you have a django project running on different servers each with its own asynchronous tasks handler this project helps you have one way to process the tasks while converting to the respective handler in the background. Currently Google Cloud Tasks and Celery are supported.
代码示例
To set up you will need to add the following to your settings.py file
DEFAULT_ASYNC_TASKS_HANDLER = env('DEFAULT_ASYNC_TASKS_HANDLER')
This can either beCELERY
orGOOGLE_CLOUD_TASKS
.
如果您正在使用芹菜,请记住设置其他设置,如CELERY_BROKER_URL
,CELERY_RESULT_BACKEND
,CELERY_ACCEPT_CONTENT
,CELERY_TASK_SERIALIZER
,CELERY_RESULT_SERIALIZER
,CELERY_TIMEZONE
{{cd7> 将视图添加到项目中网址.py文件如下 假设你有如下任务 要异步处理任务,请执行以下操作 To install the package do
标签:from django.urls import path, include
urlpatterns = [
...
path('tasks/', include('django_swappable_tasks.urls')),
...
]
from django_swappable_tasks.utils import process_task_asynchronously
from my project.tasks import my_blocking_task
task_kwargs = {"name": "John Doe", "age" : 18}
process_task_asynchronously(my_blocking_task, "google_default_queue", task_args=[], task_kwargs= task_kwargs)
安装
pip install django_swappable_tasks
You can also directly install from The GitHub Repository推荐PyPI第三方库