aiohttp: set maximum number of requests per second

前端 未结 4 1061
耶瑟儿~
耶瑟儿~ 2020-12-05 07:11

How can I set maximum number of requests per second (limit them) in client side using aiohttp?

4条回答
  •  孤街浪徒
    2020-12-05 07:43

    I found one possible solution here: http://compiletoi.net/fast-scraping-in-python-with-asyncio.html

    Doing 3 requests at the same time is cool, doing 5000, however, is not so nice. If you try to do too many requests at the same time, connections might start to get closed, or you might even get banned from the website.

    To avoid this, you can use a semaphore. It is a synchronization tool that can be used to limit the number of coroutines that do something at some point. We'll just create the semaphore before creating the loop, passing as an argument the number of simultaneous requests we want to allow:

    sem = asyncio.Semaphore(5)
    

    Then, we just replace:

    page = yield from get(url, compress=True)
    

    by the same thing, but protected by a semaphore:

    with (yield from sem):
        page = yield from get(url, compress=True)
    

    This will ensure that at most 5 requests can be done at the same time.

提交回复
热议问题