Threading in Django is not working in production

后端 未结 1 1398
清歌不尽
清歌不尽 2020-12-07 02:51

I have a function in my Django views.py that looks like this.

def process(request):
form = ProcessForm(request.POST, request.FILES)
if form.is_valid():
    i         


        
相关标签:
1条回答
  • 2020-12-07 03:28

    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.

    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'))
        ...
    

    On the subsequent page

    task_id = request.session.get('task_id')
    if task_id:
        task = AsyncResult(task_id)
    
    0 讨论(0)
提交回复
热议问题