Django可以做多线程工作吗? [英] Can Django do multi-thread works?

查看:1363
本文介绍了Django可以做多线程工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,Django可以做多线程工作吗?



这是我要做的:点击网页上的一个按钮,然后在models.py中启动运行一些功能,例如抓取一些数据从互联网,完成后,它返回给用户的结果。



我想知道我必须打开一个新的线程来执行model.py中的函数,有人可以告诉我怎么做吗?非常感谢。

解决方案


  1. 是的,它可以多线程,但一般来说,做等同的您可以阅读关于芹菜-django教程的内容。 / a>

  2. 您很难想要强制用户等待网站。

以下是您正在描述的示例。

 用户发送请求
Django receive =>产生一个线程来做别的事情。
主线程完成&&其他线程完成
...(之后完成两个任务)
响应作为一个包发送给用户。

更好的方法:

 用户发送请求
Django receive =>让芹菜知道嘿!这样做!
主线程完成
响应发送给用户
...(稍后)
用户收到交易余额


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 to run, for example, crawl some data from internet, when finished, it returns to user the results.

I wonder that I have to open a new thread to execute the functions in model.py, can anybody tell me how to do it? Thank you very much.

解决方案

  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 

这篇关于Django可以做多线程工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆