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

发布于 2024-08-05 07:46:06 字数 170 浏览 4 评论 0原文

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

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

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

等你爱我 2024-08-12 07:46:06

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

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

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.

edited to reflect comments by: sos-skyl

情话难免假 2024-08-12 07:46:06

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

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

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 Twisted framework, there are also asynchronous server building blocks in Python's standard library: previously asyncore and asynchat, but now also asyncio.

紅太極 2024-08-12 07:46:06

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

对我来说,我的 Twisted 服务器与安装在房屋和企业中的许多传感器进行通信,用于监控电力使用情况。它存储数据并将最近的数据和状态保存在内存中方便的 Python 类中。 django 通过 xmlrpc 发出的请求获取此状态,并且可以向用户呈现最新数据。我的 Gridspy 东西仍在开发中,因此 your.gridspy.co.nz 的实际站点还处于 alpha 测试阶段。

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

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.

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.

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.

暖心男生 2024-08-12 07:46:06

在 Twisted 中,您可以实现自己的协议。姜戈当然做不到这一点。

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

薔薇婲 2024-08-12 07:46:06

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

顺便说一句:我希望将此演示移至谷歌应用程序引擎上
作为更完整的服务
我的生活平静了一些。

You could use WHIFF instead of either :). Check out
http://aaron.oirt.rutgers.edu/myapp/gfChat/nucularChatRoom
which 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.

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.

来世叙缘 2024-08-12 07:46:06

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文