使用 tornado.gen.Task 并没有获得异步的效果
原文连接: http://python.jobbole.com/84944/?utm_source=tuicool&utm_medium=referral#article-comment
代码
class AsyncTaskHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous @tornado.gen.coroutine def get(self, *args, **kwargs): response = yield tornado.gen.Task(self.ping, ' www.cnode.com') print 'response', response self.finish('hello') @tornado.gen.coroutine def ping(self, url): os.system("ping -c 2 {}".format(url)) return 'after
输出
Server Software: TornadoServer/4.3
Server Hostname: 127.0.0.1
Server Port: 5000Document Path: /async/task
Document Length: 5 bytesConcurrency Level: 5
Time taken for tests: 0.049 seconds
Complete requests: 5
Failed requests: 0
Total transferred: 985 bytes
HTML transferred: 25 bytes
Requests per second: 101.39 [#/sec] (mean)
Time per request: 49.314 [ms] (mean)
Time per request: 9.863 [ms] (mean, across all concurrent requests)
Transfer rate: 19.51 [Kbytes/sec] received这个输出不对, 同样的代码, 我测试如下
Concurrency Level: 5
Time taken for tests: 6.599 seconds
Complete requests: 5
Failed requests: 0
Total transferred: 985 bytes
HTML transferred: 25 bytes
Requests per second: 0.76 [#/sec] (mean)
Time per request: 6599.359 [ms] (mean)
Time per request: 1319.872 [ms] (mean, across all concurrent requests)
Transfer rate: 0.15 [Kbytes/sec] received哪儿出了问题? 不知道是怎么测试出这个结果的? 想不明白.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
os.system 是 block 的
Tornado 的 block 是基于网络的异步 IO
要想异步,用 http://www.tornadoweb.org/en/stable/process.html#tornado.process.Subprocess
为什么我使用时老师报错,用的是tornado==6.0.1
'tornado.gen' has no attribute 'Task'