tornado非阻塞式Http请求AsyncHTTPClient

tornado是一个并发框架,在写IO密集型的程序时,非阻塞式请求才能最大程度发挥框架的能力。
AsyncHTTPClient的使用官方给的例子是:

Example usage::

        def handle_response(response):
            if response.error:
                print("Error: %s" % response.error)
            else:
                print(response.body)

        http_client = AsyncHTTPClient()
        http_client.fetch("http://www.google.com/", handle_response)

现在有一个疑问:每次请求都需要新建一个AsyncHTTPClient对象吗?

The constructor for this class is magic in several respects: It
actually creates an instance of an implementation-specific
subclass, and instances are reused as a kind of pseudo-singleton
(one per .IOLoop). The keyword argument force_instance=True
can be used to suppress this singleton behavior.

这是该类的一段介绍,可以发现只要构造对象时不使用force_instance的参数,该类会表现出单例类的行为,所以每次新建对象没有问题。

AsyncHTTPClient.configure(None, defaults=dict(user_agent="MyUserAgent"))
# or with force_instance:
client = AsyncHTTPClient(force_instance=True,
     defaults=dict(user_agent="MyUserAgent"))

下面是类的构造方法:

	@classmethod
    def _async_clients(cls):
        attr_name = '_async_client_dict_' + cls.__name__
        if not hasattr(cls, attr_name):
            setattr(cls, attr_name, weakref.WeakKeyDictionary())
        return getattr(cls, attr_name)

    def __new__(cls, force_instance=False, **kwargs):
        io_loop = IOLoop.current()
        if force_instance:
            instance_cache = None
        else:
            instance_cache = cls._async_clients()
        if instance_cache is not None and io_loop in instance_cache:
            return instance_cache[io_loop]
        instance = super(AsyncHTTPClient, cls).__new__(cls, **kwargs)
        # Make sure the instance knows which cache to remove itself from.
        # It can't simply call _async_clients() because we may be in
        # __new__(AsyncHTTPClient) but instance.__class__ may be
        # SimpleAsyncHTTPClient.
        instance._instance_cache = instance_cache
        if instance_cache is not None:
            instance_cache[instance.io_loop] = instance
        return instance

weakref 参见:https://www.cnblogs.com/marsggbo/p/14831456.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值