• AsyncHTTPClient


    针对HTTP请求,Python的库Requests是最好用的库,没有之一。官网宣称:HTTP for Human。然而,在tornado中直接使用requests将会是一场恶梦。

    requests的请求会block整个服务进程。

    AsyncHTTPClient是 tornado.httpclinet 提供的一个异步http客户端。使用也比较简单。与服务进程一样,AsyncHTTPClient也可以callback和yield两种使用方式。
    前者不会返回结果,后者则会返回response。 如果请求第三方服务是同步方式,同样会杀死性能。
    class AsyncHandler(tornado.web.RequestHandler):
      @tornado.web.asynchronous
      def get(self, *args, **kwargs):
    
        url = 'https://api.github.com/'
        http_client = tornado.httpclient.AsyncHTTPClient()
        http_client.fetch(url, self.on_response)
        self.finish('It works')
    
      @tornado.gen.coroutine
      def on_response(self, response):
        print response.code

    同样,为了获取response的结果,只需要yield函数。

    class AsyncResponseHandler(tornado.web.RequestHandler):
      @tornado.web.asynchronous
      @tornado.gen.coroutine
      def get(self, *args, **kwargs):
    
        url = 'https://api.github.com/'
        http_client = tornado.httpclient.AsyncHTTPClient()
        response = yield tornado.gen.Task(http_client.fetch, url)
        print response.code
        print response.body

    参考链接:https://www.jb51.net/article/87459.htm

  • 相关阅读:
    KETTLE集群搭建
    初识spark
    利用python操作mrjob实例---wordcount
    hive 优化 (转)
    hive权限管理
    hadoop常用操作命令
    hive的分桶
    Hive HQL基本操作
    hadoop--hive数据仓库
    Hive配置项的含义详解
  • 原文地址:https://www.cnblogs.com/topass123/p/13404342.html
Copyright © 2020-2023  润新知