Can Django do multi-thread works?

后端 未结 3 866
無奈伤痛
無奈伤痛 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:37

    1. Yes it can multi-thread, but generally one uses Celery to do the equivalent. You can read about how in the celery-django tutorial.
    2. It is rare that you actually want to force the user to wait for the website. While it's better than risks a timeout.

    Here's an example of what you're describing.

    User sends request
    Django receives => spawns a thread to do something else.
    main thread finishes && other thread finishes 
    ... (later upon completion of both tasks)
    response is sent to user as a package.
    

    Better way:

    User sends request
    Django receives => lets Celery know "hey! do this!"
    main thread finishes
    response is sent to user
    ...(later)
    user receives balance of transaction 
    

提交回复
热议问题