prequests.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:dcard-spider 作者: leVirve 项目源码 文件源码
def imap_unordered(requests, stream=True, pool=None, size=2, exception_handler=None):
    """Concurrently converts a generator object of Requests to
    a generator of Responses.

    :param requests: a generator of Request objects.
    :param stream: If False, the content will not be downloaded immediately.
    :param size: Specifies the number of requests to make at a time. default is 2
    :param exception_handler: Callback function, called when exception occured. Params: Request, Exception
    """

    def send(r):
        return r.send(stream=stream)

    pool = pool if pool else Pool(size)

    with contextlib.closing(Pool(size)) as pool:
        for request in pool.imap_unordered(send, requests):
            if request.response is not None:
                yield request.response
            elif exception_handler:
                exception_handler(request, request.exception)

    if not pool:
        pool.close()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号