使用 Twisted 和 Comet 将命令结果流式传输回浏览器
我正在编写一个应用程序,它将服务器上激活的 python 脚本的输出(我指的是 sys.stdout 和 sys.stderr)实时流式传输到浏览器。
网站上的用户将被允许选择要运行的脚本,激发和终止他们选择的脚本,并更改一些参数,因此我需要网站上每个用户有一个不同的线程(用户 A 可以启动、停止和更改脚本,而用户 B 可以使用不同的脚本执行相同的操作)。
我知道我需要为网络客户端使用 comet,并且看到项目的其余部分是用 python 编写的,我想为服务器使用twisted,但是我不太确定下一步需要做什么!
有大量选项(Divmod Mantissa、Divmod Nevow、twisted.web、STOMP 等),其中一些选项比其他选项有更好的文档记录,这使得整个事情变得相当棘手!
我有一个在 Orbited 上使用 stompservice 的工作演示,使用 Orbited.TCPSocket 进行 javascript 方面的工作,但是我开始认为 STOMP 通道模型不适用于多线程、多运行脚本(除非我打开一个每次运行新通道,但这似乎是通道模型的错误使用)。
谁能指出我正确的方向,或者一些我可以学习的示例代码?
谢谢!
I'm writing an application that streams the output (by this I mean both sys.stdout and sys.stderr) of a python script excited on the server, in real time to the browser.
The users on the site will be allowed to select the script to run, excite and kill their chosen script, and change some parameters, so I will need a different thread per user on the site (user A can start, stop and change a script, whilst user B can do the same with a different script).
I know I need to use comet for the web clients, and seeing as the rest of the project is written in python, I'd like to use twisted for the server, however I'm not really sure of what I need to do next!
There are a daunting number of options (Divmod Mantissa, Divmod Nevow, twisted.web, STOMP, etc), and some are better documented that others, making the whole thing rather tricky!
I have a working demo using stompservice on orbited, using Orbited.TCPSocket for the javascript side of things, however I'm starting to think that STOMPs channel model isn't going to work for multithreading, multi-running scripts (unless I open a new channel per run, but that seems like the wrong use of the channel model).
Can anyone point me in the right direction, or some sample code I can learn from?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Nevow Athena 是一个专门针对 AJAX 和 COMET 应用程序的框架,理论上正是您正在寻找的东西。
然而,我不确定它目前是否得到了很好的使用或支持——查看邮件列表流量和谷歌搜索结果表明它可能没有得到很好的使用或支持。
您可以查看一些教程来帮助您做出决定:
“官方”网站上的一个:http://divmod.org/trac/wiki/DivmodNevow/Athena/Tutorials/LiveElement
还有我发现的另一个:
http://divmodsphinx.funsize.net/nevow/chattutorial/part01/index.html html
后者的代码似乎包含在 Nevow 发行版中(我认为...)
Nevow Athena is a framework specifically for AJAX and COMET applications and in theory is exactly the sort of thing you are looking for.
However, I am not sure that it is well used or supported at this time - looking at mailing list traffic and google search results suggests that it may not be.
There are a couple of tutorials you could look at to help you decide on it:
one on the 'official' site: http://divmod.org/trac/wiki/DivmodNevow/Athena/Tutorials/LiveElement
and one other that I found:
http://divmodsphinx.funsize.net/nevow/chattutorial/part01/index.html
The code for the latter seems to be included in the Nevow distribution when you download it under /doc/listings/partxx (I think...)
您可以通过保持 http 连接打开并附加更新 dom 内容的 javascript 块来实现非常简单的“HTTP 流”。这是有效的,因为浏览器会在“脚本”块到达时对其进行评估。
我不久前写了一篇博客文章,其中包含一个使用扭曲且很少行的 javascript 的运行示例: Simple HTTP Streaming with扭曲& Javascript
您可以轻松地将此模式与发布者/订阅者模式混合以使其成为多用户等。我使用此模式通过网络观看实时日志流。
You can implement a very simple "HTTP streaming" by keeping the http connection open and appending javascript chunks that update the dom contents. This works since the browser evaluates the "script" chunks as they arrive.
I wrote a blog entry a while ago with a running example using twisted and very few lines of javascript: Simple HTTP streaming with Twisted & Javascript
You can easily mix this pattern with a publisher/subscriber pattern to make it multiuser, etc. I use this pattern to watch live log streams via web.
使用 Twisted 为长轮询客户端提供服务的示例是 slosh。这可能不是您想要的,但因为它不是一个大型框架,所以它可以帮助您了解如何使用 Twisted。
An example of serving for long-polling clients with Twisted is slosh. This might not be what you want, but because it's not a large framework, it can help you figure out how to use Twisted.