原文连接: 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 哪儿出了问题? 不知道是怎么测试出这个结果的? 想不明白.
os.system 是 block 的
Tornado 的 block 是基于网络的异步 IO
要想异步,用 http://www.tornadoweb.org/en/stable/process.html#tornado.process.Subprocess