使用 tornado.gen.Task 并没有获得异步的效果

  1. 原文连接: http://python.jobbole.com/84944/?utm_source=tuicool&utm_medium=referral#article-comment

  2. 代码

    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
    
  3. 输出

    Server Software: TornadoServer/4.3
    Server Hostname: 127.0.0.1
    Server Port: 5000

    Document Path: /async/task
    Document Length: 5 bytes

    Concurrency 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

  4. 这个输出不对, 同样的代码, 我测试如下

    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

  5. 哪儿出了问题? 不知道是怎么测试出这个结果的? 想不明白.

阅读 4.7k
评论
    2 个回答
      • 1
      • 新人请关照

      为什么我使用时老师报错,用的是tornado==6.0.1
      'tornado.gen' has no attribute 'Task'

        撰写回答

        登录后参与交流、获取后续更新提醒

        相似问题
        推荐文章