5

我从如下视图调用管理命令:

from django.http import JsonResponse
from django.core.management import call_command

def index(request):
    call_command('mymanagementcommand')
    response = {'result': 'success',
                'message': 'thank you, come again'}
    return JsonResponse(response)

我不想在继续查看视图并返回响应之前等待我的管理命令完成。在这种情况下,“成功”仅表示命令已被调用,并且不关心该命令是否成功运行。

有没有一种 djangoy 方式让我在后台启动它而不等待它?

谢谢!

4

1 回答 1

0

将 call_command 添加到单独的 python 线程。

import threading

class CustomThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        call_command("mymanagementcommand")

def index(request):
   CustomThread().start()

于 2022-02-01T10:01:20.933 回答