python Twisted(异步服务器)与 Django(或任何其他框架)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1412169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:11:14  来源:igfitidea点击:

Twisted(asynch server) vs Django(or any other framework)

pythondjangoasynchronoustwistedreal-time

提问by fanar

I need help understanding what the advantage of using an asynch framework is. Suppose I want to develop a simple chat web app. Why cant I write python code in the Django framework that does long polling where I dont send a response back the server until someone enters a new msg. What does Twisted provide that gives it an advantage for real-time apps like the chat app?

我需要帮助了解使用异步框架的优势是什么。假设我想开发一个简单的聊天网络应用程序。为什么我不能在执行长轮询的 Django 框架中编写 python 代码,在那里我不向服务器发送响应,直到有人输入新的味精。Twisted 提供了什么使其成为聊天应用程序等实时应用程序的优势?

Sorry I am obviously little confused about the need for an asynchronous framework.

抱歉,我显然对异步框架的需求有点困惑。

回答by stonemetal

First off Django is a framework for writing web apps so it provides ORM, html templating, it requires running an http server etc. Twisted helps to write much lower level code than that. You could use twisted to write the http server Django runs on. If you use Django you are limited to http model, with twisted it could be communicating in any protocol you like including push protocols. So for your chat example you get a server that scales better since it can push comments to people who have logged in VS with django every client having to poll repeatedly.

首先,Django 是一个用于编写 Web 应用程序的框架,因此它提供 ORM、html 模板,它需要运行一个 http 服务器等。Twisted 有助于编写比这低得多的代码。您可以使用twisted 编写运行Django 的http 服务器。如果您使用 Django,则您只能使用 http 模型,而 Twisted 可以使用您喜欢的任何协议进行通信,包括推送协议。因此,对于您的聊天示例,您将获得一个扩展性更好的服务器,因为它可以向使用 django 登录 VS 的人推送评论,每个客户端都必须反复轮询。

edited to reflect comments by: sos-skyl

编辑以反映评论者:sos-skyl

回答by Vinay Sajip

Asynchronous servers support much larger numbers of simultaneous client connections. More conventional servers come up against thread and process limits when servicing large number of concurrent clients, particularly those with long-lived connections. Async servers can also provide better performance as they avoid the overheads of e.g. thread context switching.

异步服务器支持更多的并发客户端连接。更传统的服务器在为大量并发客户端提供服务时会遇到线程和进程限制,尤其是那些具有长期连接的客户端。异步服务器还可以提供更好的性能,因为它们避免了例如线程上下文切换的开销。

As well as the Twistedframework, there are also asynchronous server building blocks in Python's standard library: previously asyncoreand asynchat, but now also asyncio.

除了Twisted框架之外,Python 的标准库中还有异步服务器构建块:以前是asyncoreasynchat,但现在也有asyncio.

回答by Tom Leys

The biggest advantage for me is that Twisted gives me an application that has state, and can communicate with many different clients using many protocols.

对我来说最大的优势是 Twisted 为我提供了一个具有状态的应用程序,并且可以使用多种协议与许多不同的客户端进行通信。

For me, my Twisted server communicates with a number of sensors installed in houses and businesses that monitor power usage. It stores the data and keeps recent data and state in handy-dandy python classes in memory. Requests via xmlrpc from django get this state and can present recent data to the user. My Gridspy stuff is still in development so the actual site at your.gridspy.co.nz is a bit pre-alpha.

对我来说,我的 Twisted 服务器与安装在房屋和企业中的许多传感器进行通信,以监控电源使用情况。它存储数据并将最近的数据和状态保存在内存中方便的 Python 类中。来自 django 的通过 xmlrpc 的请求获得此状态,并且可以向用户显示最近的数据。我的 Gridspy 东西仍在开发中,所以 your.gridspy.co.nz 上的实际站点有点 pre-alpha。

The best part is that you need surprisingly little code to make an effective server. An amazing amount of the work is done for you.

最好的部分是您只需很少的代码就可以制作一个有效的服务器。为您完成了大量的工作。

回答by Skylar Saveland

In twisted you can implement protocols of your own. Django certainly can't do this.

在 Twisted 中,您可以实现自己的协议。Django 肯定做不到这一点。

回答by Aaron Watters

You could use WHIFFinstead of either :). Check out http://aaron.theitroadt.rutgers.edu/myapp/gfChat/nucularChatRoomwhich uses a javascript polling loop with json to check for server updates. You could probably do something similar in Django, but I don't know how because I gave up on Django.

您可以使用WHIFF而不是 :)。查看 http://aaron.theitroadt.rutgers.edu/myapp/gfChat/nucularChatRoom,它使用带有 json 的 javascript 轮询循环来检查服务器更新。你可能可以在 Django 中做类似的事情,但我不知道怎么做,因为我放弃了 Django。

btw: I'm hoping to move this demo onto the google app engine as a more complete service when my life calms down a bit.

顺便说一句:当我的生活平静下来时,我希望将此演示作为更完整的服务移动到谷歌应用程序引擎上。

回答by DrBloodmoney

If you'd like to look at some source for integrating Twisted and Django, have a look at Yardbird.

如果您想查看一些用于集成 Twisted 和 Django 的源代码,请查看Yardbird