Twisted:服务器端进程完成时通知客户端
我正在使用 Twisted 编写网络服务器。该服务器执行的其中一项任务需要很长时间(约 5 分钟)。我希望能够有效地通知客户该任务已完成。
我已经研究过使用 Comet/长轮询,但在我的一生中,我无法让浏览器在收到数据时呈现数据。
为了构建此机制的原型,我编写了以下内容:
clock.py
from twisted.internet import reactor, task
from twisted.web.static import File
from twisted.web.server import Site
from twisted.web import server
from twisted.web.resource import Resource
import time
class Clock(Resource):
def __init__(self):
self.presence=[]
loopingCall = task.LoopingCall(self.__print_time)
loopingCall.start(1, False)
Resource.__init__(self)
def render_GET(self, request):
print "request from",request.getClientIP()
request.write(time.ctime())
self.presence.append(request)
return server.NOT_DONE_YET
def __print_time(self):
print "tick"
for p in self.presence:
print "pushing data to",p.getClientIP()
p.write(time.ctime())
root = Resource()
clock = ClockPage()
index = File("index.html")
root.putChild("index.html",index)
root.putChild("clock",clock)
factory = Site(root)
reactor.listenTCP(8080, factory)
reactor.run()
index.html
<html>
<head>
</head>
<body>
<div id="data">Hello</div>
<script type="text/javascript">
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(xhttp.readyState == 4 && xhttp.status == 200){
alert(xhttp.responseText);
document.getElementById("data").innerHTML=xhttp.responseText;
}
};
xhttp.open("GET","clock",true);
xhttp.send(null);
</script>
</body>
</html>
我在服务器端所做的就是调用 request.write 随着时间的推移,每一秒。
在客户端,我所做的就是打开一个 XMLHTTPRequest 到适当的资源,并在 .readyState == 4
和 .status 时将
。responseText
直接转储到 div 中== 200
问题是:div 永远不会被覆盖,警报也永远不会被调用。
我一直在阅读有关使用 multipart/x-mixed-replace
的内容,但我不太确定如何使用它。任何关于在扭曲中实现此类事情的教程或文档的指针将不胜感激。
I'm using Twisted to write a web server. One of the tasks that this server performs takes a long time (~5 minutes). I would like to be able to efficiently notify the client that this task has completed.
I have looked into using Comet/long polling, but for the life of me I can't get the browser to render the data when it receives it.
To prototype this mechanism, I wrote the following:
clock.py
from twisted.internet import reactor, task
from twisted.web.static import File
from twisted.web.server import Site
from twisted.web import server
from twisted.web.resource import Resource
import time
class Clock(Resource):
def __init__(self):
self.presence=[]
loopingCall = task.LoopingCall(self.__print_time)
loopingCall.start(1, False)
Resource.__init__(self)
def render_GET(self, request):
print "request from",request.getClientIP()
request.write(time.ctime())
self.presence.append(request)
return server.NOT_DONE_YET
def __print_time(self):
print "tick"
for p in self.presence:
print "pushing data to",p.getClientIP()
p.write(time.ctime())
root = Resource()
clock = ClockPage()
index = File("index.html")
root.putChild("index.html",index)
root.putChild("clock",clock)
factory = Site(root)
reactor.listenTCP(8080, factory)
reactor.run()
index.html
<html>
<head>
</head>
<body>
<div id="data">Hello</div>
<script type="text/javascript">
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(xhttp.readyState == 4 && xhttp.status == 200){
alert(xhttp.responseText);
document.getElementById("data").innerHTML=xhttp.responseText;
}
};
xhttp.open("GET","clock",true);
xhttp.send(null);
</script>
</body>
</html>
What I've been doing on the server side is calling request.write
with the time, every second.
On the client side, all I do is open a XMLHTTPRequest to the appropriate resource and dump the responseText
directly into a div whenever .readyState == 4
and .status == 200
.
The problem is: the div never gets overwritten and the alert is never called.
I keep reading about using multipart/x-mixed-replace
, but I'm not really sure how to use it. Any pointers to tutorials or documentation on implementing this sort of thing in twisted would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑将
p.finish()
添加到循环中,以便请求真正完成。当前的实现将永远挂起。Consider adding a
p.finish()
to your loop, so that the request actually completes. The current implementation will hang forever.改用“HTTP 流”怎么样?我已经成功地使用它将日志从服务器“流”到“监听”浏览器。这是一个使用扭曲和一些js的简单实现: http://codelab.ferrarihaines.com/archives/ 161
What about using "HTTP streaming" instead? I have used it successfully to "stream" logs from the server to the "listening" browsers. Here is a simple implementation using twisted and a bit of js: http://codelab.ferrarihaines.com/archives/161