如何正确地在Python中发出Api Api请求?

时间:2019-06-12 21:24:03

标签: python python-asyncio aiohttp

我不是python专家,所以尝试发出异步api请求时遇到了麻烦,它抛出“ RuntimeError:事件循环已关闭”。

我向API发出很多请求,有时它返回状态码429(请求太多),所以我想要的是发生这种情况时,我想继续尝试该请求,直到它返回状态码200并继续该过程。

import requests
import asyncio
import aiohttp

CONCURRENCY = 3
TIMEOUT = 40000

async def fetch(session, sem, obj):
    async with sem:
        async with session.get(obj['url'], headers={}, params={}) as resp:
            #print(await response.json())

            if resp.status == 429: # too many consecutive requests
                print('Too many requests. Waiting some seconds to continue...')
                await sleep(5)

                while True:
                    r = await requests.get(obj['url'], auth=('xxxxx',''))
                    if r.status != 429:
                        break
                    await sleep(6)

                return await r


async def call_api(list_obj):
    try:
        sem = asyncio.Semaphore(CONCURRENCY)
        async with aiohttp.ClientSession(
            auth=aiohttp.BasicAuth(login='xxxxx', password='')) as session:
                responses = await asyncio.gather(*(
                    asyncio.wait_for(fetch(session, sem, i), TIMEOUT)
                    for i in list_obj
        ))

        return responses
    except :
        logger.critical('API request problem: ' + traceback.format_exc())



loop = asyncio.get_event_loop()
future = asyncio.ensure_future(call_api(array))
data = loop.run_until_complete(future)
loop.close()

它抛出“ RuntimeError:事件循环已关闭”。

0 个答案:

没有答案