這是我的決議器代碼的片段。它異步執行 120 個請求。但是,每個回應都會回傳 429“請求過多”錯誤。我如何讓它“變慢”,所以 api 不會拒絕我?
def get_tasks(self, session):
tasks = []
for url in self.list_of_urls:
tasks.append(asyncio.create_task(session.get(url, ssl=False)))
return tasks
async def get_symbols(self):
print('Parsing started')
async with aiohttp.ClientSession() as session:
tasks = self.get_tasks(session)
responses = await asyncio.gather(*tasks)
for response in responses:
response = await response.json()
print(response)
錯誤:
{'message': 'Too many requests'}
{'message': 'Too many requests'}
{'message': 'Too many requests'}
{'message': 'Too many requests'}
{'message': 'Too many requests'}
...
uj5u.com熱心網友回復:
嘗試使用asyncio.Semaphore
:
# Initialize a semaphore object with a limit of 3 (max 3 downloads concurrently)
limit = asyncio.Semaphore(3)
async def make_one_request(url):
async with limit:
return await session.get(url, ssl=False)
def get_tasks(self, session):
tasks = []
for url in self.list_of_urls:
tasks.append(asyncio.create_task(make_one_request(url)))
return tasks
async def get_symbols(self):
print("Parsing started")
async with aiohttp.ClientSession() as session:
tasks = self.get_tasks(session)
responses = await asyncio.gather(*tasks)
for response in responses:
response = await response.json()
print(response)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/508491.html
上一篇:如何使用async/await進行非異步io操作?比如get/putinleveldb
下一篇:異步Dart功能無法正常作業