Can Django do multi-thread works?

后端 未结 3 864
無奈伤痛
無奈伤痛 2020-11-30 01:38

I have a question, that can Django do multi-thread works?

Here is what I want to do: click a button on a web page, then there are some functions in model.py starts

3条回答
  •  醉梦人生
    2020-11-30 02:33

    If you don't want to add some overkill framework to your project, you can simply use subprocess.Popen:

    def my_command(request):
        command = '/my/command/to/run'  # Can even be 'python manage.py somecommand'
        subprocess.Popen(command, shell=True)
        command = '/other/command/to/run'
        subprocess.Popen(command, shell=True)
        return HttpResponse(status=204)
    

    [edit] As mentioned in the comments, this will not start a background task and return the HttpResponse right away. It will execute both commands in parallel, and then return the HttpResponse once both are complete. Which is what OP asked.

提交回复
热议问题