为 asyncio aiohttp 添加超时

问题描述 投票:0回答:1

我目前正在使用以下代码来查询 api 中的数据

import asyncio

async def get_data(session, ticker):
    while True:
        try:
            async with session.get(url=f'api.exec.com') as response:
                if response.status == 200:
                    data = await response.json()
                    try:
                        df = pd.DataFrame(data)
                    except ValueError:
                        print("Data is not a valid float. Retrying...")
                else:
                    print("Received non-200 status code. Retrying...")
            await asyncio.sleep(1)  
        except Exception as e:
            print("Unable to get url {} due to {}. Retrying...".format(ticker, e.__class__))
            await asyncio.sleep(1)


async def main_loop(ticker_list):
    async with aiohttp.ClientSession() as session:
        ret = await asyncio.gather(*[get_data(session, tick) for tick in ticker_list])
        return ret

datas = ['x1', 'x2', 'x3', 'x4']
results = asyncio.run(main(datas))
print(results)

我想给session.get添加超时。

如果我使用普通的 python 请求,我可以执行以下操作

session.get(url=f'api.exec.com', timeout=5)

session.get(url=f'api.exec.com', timeout=(4,7))

如何使用 asyncio / aiohttp.ClientSession 做同样的事情?

我应该在 seesion.get 行中添加 timeout = 5 吗?

import asyncio

async def get_data(session, ticker):
    while True:
        try:
            async with session.get(url=f'api.exec.com', timeout = 5) as response:
                if response.status == 200:
                    data = await response.json()
                    try:
                        df = pd.DataFrame(data)
                    except ValueError:
                        print("Data is not a valid float. Retrying...")
                else:
                    print("Received non-200 status code. Retrying...")
            await asyncio.sleep(1)  
        except Exception as e:
            print("Unable to get url {} due to {}. Retrying...".format(ticker, e.__class__))
            await asyncio.sleep(1)


async def main_loop(ticker_list):
    async with aiohttp.ClientSession() as session:
        ret = await asyncio.gather(*[get_data(session, tick) for tick in ticker_list])
        return ret

datas = ['x1', 'x2', 'x3', 'x4']
results = asyncio.run(main(datas))
print(results)

或者我应该这样做吗?我在哪里添加 timeout=aiohttp.ClientTimeout(total=5) 对象到超时?

import asyncio

async def get_data(session, ticker):
    while True:
        try:
            async with session.get(url=f'api.exec.com', timeout=aiohttp.ClientTimeout(total=5)) as response:
                if response.status == 200:
                    data = await response.json()
                    try:
                        df = pd.DataFrame(data)
                    except ValueError:
                        print("Data is not a valid float. Retrying...")
                else:
                    print("Received non-200 status code. Retrying...")
            await asyncio.sleep(1)  
        except Exception as e:
            print("Unable to get url {} due to {}. Retrying...".format(ticker, e.__class__))
            await asyncio.sleep(1)


async def main_loop(ticker_list):
    async with aiohttp.ClientSession() as session:
        ret = await asyncio.gather(*[get_data(session, tick) for tick in ticker_list])
        return ret

datas = ['x1', 'x2', 'x3', 'x4']
results = asyncio.run(main(datas))
print(results)

或者像这样?我在主函数中哪里设置超时?

import asyncio

async def get_data(session, ticker):
    while True:
        try:
            async with session.get(url=f'api.exec.com') as response:
                if response.status == 200:
                    data = await response.json()
                    try:
                        df = pd.DataFrame(data)
                    except ValueError:
                        print("Data is not a valid float. Retrying...")
                else:
                    print("Received non-200 status code. Retrying...")
            await asyncio.sleep(1)  
        except Exception as e:
            print("Unable to get url {} due to {}. Retrying...".format(ticker, e.__class__))
            await asyncio.sleep(1)


async def main_loop(ticker_list):
    timeout = aiohttp.ClientTimeout(total=5)  # Setting timeout here
    async with aiohttp.ClientSession(timeout=timeout) as session:
        ret = await asyncio.gather(*[get_data(session, tick) for tick in ticker_list])
        return ret

datas = ['x1', 'x2', 'x3', 'x4']
results = asyncio.run(main(datas))
print(results)

asynchronous python-requests timeout python-asyncio aiohttp
1个回答
0
投票

超时= 5

根据文档,这是无效的,并且在 v4 中根本不起作用。

您给出的其他2个选项是正确的。

第一个单独设置每个请求的超时(并将覆盖 ClientSession 中设置的超时)。

第二个在会话中全局设置它,因此它自动应用于该会话中的所有请求(如果没有覆盖)。

© www.soinside.com 2019 - 2024. All rights reserved.