且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django 中的线程在生产中不起作用

更新时间:2023-08-22 21:57:58

错误的架构.Django 和其他网络应用程序应该产生这样的线程.正确的方法是使用任务队列创建异步任务.django 最流行的任务队列恰好是 Celery.

Wrong architecture. Django and other web apps should be spawning threads like this. The correct way is to create an async task using a task queue. The most popular task queue for django happens to be Celery.

mart:processing 页面随后应检查异步结果以确定任务是否已完成.粗略的草图如下.

The mart:processing page should then check the async result to determine if the task has been completed. A rough sketch is as follows.

from celery.result import AsynResult
from myapp.tasks import my_task

...
if form.is_valid():
    ...
    task_id = my_task()
    request.session['task_id']=task_id
    return HttpResponseRedirect(reverse('mart:processing'))
    ...

在下一页

task_id = request.session.get('task_id')
if task_id:
    task = AsyncResult(task_id)