客户端完成从 Tornado Web 服务器下载后如何调用函数? [英] How do you call function after client finishes download from tornado web server?
问题描述
当且仅当客户端成功完成我使用 Tornado 服务的文件的下载时,我希望能够运行一些清理功能.
I would like to be able to run some cleanup functions if and only if the client successfully completes the download of a file I'm serving using Tornado.
我安装了 firefox 节流工具,让它将连接减慢到拨号速度,并安装了这个处理程序来生成一堆垃圾随机文本:
I installed the firefox throttle tool and had it slow the connection down to dialup speed and installed this handler to generate a bunch of rubbish random text:
class CrapHandler(BaseHandler):
def get(self, token):
crap = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(100000))
self.write(crap)
print "done"
我在提出请求后立即从 tornado 得到以下输出:
I get the following output from tornado immediately after making the request:
done
I 100524 19:45:45 web:772] 200 GET /123 (192.168.45.108) 195.10ms
然后客户端缓慢下载大约 20 秒.我预计它会在客户端完成后打印完成".
The client then plods along downloading for about 20 seconds. I expected that it would print "done" after the client was done.
此外,如果我执行以下操作,我会得到几乎相同的结果:
Also, if I do the following I get pretty much the same result:
class CrapHandler(BaseHandler):
@tornado.web.asynchronous
def get(self, token):
crap = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(100000))
self.write(crap)
self.finish()
print "done"
我在这里遗漏了一些基本的东西吗?龙卷风甚至可以支持我正在尝试做的事情吗?如果没有,是否有替代方案?
Am I missing something fundamental here? Can tornado even support what I'm trying to do? If not, is there an alternative that does?
推荐答案
我相信您正在寻找可以在 on_connection_close
可以覆盖的请求处理程序方法.
I believe you are looking for something that runs in the on_connection_close
request handler method which you can override.
记住,如果你跑在nginx后面,tornado会立即响应nginx,nginx会慢慢响应客户端.
Keep in mind that if you are running behind nginx, tornado will respond to nginx immediately, and nginx will slowly respond to the client.
另外,请记住,添加 @tornado.web.asynchronous
实际上不会使请求异步.它只设置了使用 tornado.http.AsyncHTTPClient
的请求.
Also, keep in mind that adding @tornado.web.asynchronous
doesn't actually make a request asynchronous. It only set's up the request to use tornado.http.AsyncHTTPClient
.
这篇关于客户端完成从 Tornado Web 服务器下载后如何调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!