【发布时间】:2020-08-31 17:07:08
【问题描述】:
我正在尝试对 API 进行并行调用。 API 在停止前限制为每分钟 1,200 次调用。在低于限制的情况下进行异步的最有效方法是什么?
def remove_html_tags(text):
"""Remove html tags from a string"""
import re
clean = re.compile('<.*?>')
return re.sub(clean, ' ', text)
async def getRez(df, url):
async with aiohttp.ClientSession() as session:
auth = aiohttp.BasicAuth('username',pwd)
r = await session.get(url, auth=auth)
if r.status == 200:
content = await r.text()
text = remove_html_tags(str(content))
else:
text = '500 Server Error'
df.loc[df['url'] == url, ['RezText']] = [[text]]
df['wordCount'] = df['RezText'].apply(lambda x: len(str(x).split(" ")))
data = df[df["RezText"] != "500 Server Error"]
async def main(df):
df['RezText'] = None
await asyncio.gather(*[getRez(df, url) for url in df['url']])
loop = asyncio.get_event_loop()
loop.run_until_complete(main(data))
【问题讨论】:
标签: python async-await aiohttp