我正在尝试为Django(1.2)提供流媒体响应的"hello world".我想出了如何使用发电机和yield功能.但响应仍然不流.我怀疑有一个中间件正在捣乱它 - 也许是ETAG计算器?但我不知道如何禁用它.有人可以帮忙吗?
这是我到目前为止流媒体的"你好世界":
def stream_response(request):
resp = HttpResponse( stream_response_generator())
return resp
def stream_response_generator():
for x in range(1,11):
yield "%s\n" % x # Returns a chunk of the response to the browser
time.sleep(1)
Run Code Online (Sandbox Code Playgroud) 我有一个简单的网站(http://www.kousenit.com/twitterfollowervalue),根据一个人的Twitter粉丝计算数量.由于Twitter API一次仅返回关注者100,因此完整的过程可能涉及大量呼叫.
目前,我对运行Twitter循环的方法进行了Ajax调用.该方法看起来像(Groovy):
def updateFollowers() {
def slurper = new XmlSlurper()
followers = []
def next = -1
while (next) {
def url = "http://api.twitter.com/1/statuses/followers.xml?id=$id&cursor=$next"
def response = slurper.parse(url)
response.users.user.each { u ->
followers << new TwitterUser(... process data ...)
}
next = response.next_cursor.toBigInteger()
}
return followers
}
Run Code Online (Sandbox Code Playgroud)
这是从一个名为renderTTFV.groovy的控制器调用的.我使用原型库通过Ajax调用来调用控制器:
在我的网页上,在标题部分(JavaScript)中:
function displayTTFV() {
new Ajax.Updater('ttfv','renderTTFV.groovy', {});
}
Run Code Online (Sandbox Code Playgroud)
并且在呼叫完成时,页面正文中有一个div更新.
一切正常,但updateFollowers方法可能需要相当长的时间.有什么方法可以返回进度值吗?例如,我想在每次迭代时更新网页.我提前知道会有多少次迭代.我只是无法弄清楚在该循环中间更新页面的方法.
任何建议,将不胜感激.
ajax ×1
django ×1
groovy ×1
javascript ×1
progress-bar ×1
prototypejs ×1
python ×1
streaming ×1