Python 我能';如果上一次查询因超时而失败,则不要使用aiohttp会话

Python 我能';如果上一次查询因超时而失败,则不要使用aiohttp会话,python,python-asyncio,aiohttp,Python,Python Asyncio,Aiohttp,我有一个简单的脚本,可以创建异步任务来加载不同的页面。第一个请求因TimeoutError而失败,并导致下一个查询也失败。但是第二个有更长的超时时间,应该通过 是否可以让其他查询不失败 import aiohttp import asyncio import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)s %(levelname)s %(mess

我有一个简单的脚本,可以创建异步任务来加载不同的页面。第一个请求因TimeoutError而失败,并导致下一个查询也失败。但是第二个有更长的超时时间,应该通过

是否可以让其他查询不失败

import aiohttp
import asyncio
import logging


logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(name)s %(levelname)s %(message)s')


async def main():
    asyncio.ensure_future(
        load_page('https://www.netflix.com:5000'))

    asyncio.ensure_future(
        load_page('http://bash.im/', 10))

    asyncio.ensure_future(
        load_page('https://myshows.me/'))

    asyncio.ensure_future(
        load_page('http://www.lostfilm.tv/'))


async def load_page(url, timeout=3):
    try:
        async with session.get(url, timeout=timeout) as response:
            text = await response.text()
            print(len(text))

    except Exception:
        logging.warning(type(e))


if __name__ == '__main__':
    loop = asyncio.get_event_loop()

    conn = aiohttp.TCPConnector(limit=1)
    session = aiohttp.ClientSession(connector=conn, loop=loop)

    asyncio.ensure_future(main())
    loop.run_forever()
日志:

2017-06-26 13:57:37869使用选择器进行异步IO调试:EpollSelector
2017-06-26 13:57:41780根警告
2017-06-26 13:57:41780根警告
2017-06-26 13:57:41780根警告
2017-06-26 13:57:48780根警告

是的,这是可能的。我重写了您的代码,并使用其中一些概念来满足您的请求:

  • 我使用
    itertools.starmap
    传递多个参数,并创建传递到
    load\u页面
    函数的每个参数的列表

  • 我使用
    asyncio.gather
    将任务包装在一起,并将return\u exceptions标志更改为true,以确保不会引发异常

  • 更改了def main前的
    async
    。它立即返回收集的任务

  • 最后我结束了循环

守则:

import aiohttp
import asyncio
import logging
import itertools

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(name)s %(levelname)s %(message)s')

def main(session):
    args = [
        ('https://www.netflix.com:5000', session,),
        ('http://bash.im/', session, 10),
        ('https://myshows.me/', session,),
        ('http://www.lostfilm.tv/', session,),
    ]
    tasks = itertools.starmap(load_page, args)
    futures = map(asyncio.ensure_future, tasks)

    return asyncio.gather(*futures, return_exceptions=True)


async def load_page(url, session, timeout=3):
    try:
        async with session.get(url, timeout=timeout) as response:
            text = await response.text()
            print(len(text))
    except Exception:
        logging.warning(type(e))


if __name__ == '__main__':
    loop = asyncio.get_event_loop()

    conn = aiohttp.TCPConnector(limit=1)
    session = aiohttp.ClientSession(connector=conn, loop=loop)
    loop.run_until_complete(main(session))
    loop.close()
有关以下内容的详细信息:

有关以下内容的详细信息:


享受吧

感谢
asyncio.gather
函数。但我的主要问题仍然存在:所有4个查询都以失败告终timeout@DmitryTolkach对我来说,他们不。。。。我的程序在您的计算机上运行的输出是什么?
2017-06-29 00:39:07027使用选择器进行异步IO调试:EpollSelector 2017-06-29 00:39:10356根警告2017-06-29 00:39:10356根警告2017-06-29 00:39:10356根警告2017-06-29 00:39:17355根警告
import aiohttp
import asyncio
import logging
import itertools

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(name)s %(levelname)s %(message)s')

def main(session):
    args = [
        ('https://www.netflix.com:5000', session,),
        ('http://bash.im/', session, 10),
        ('https://myshows.me/', session,),
        ('http://www.lostfilm.tv/', session,),
    ]
    tasks = itertools.starmap(load_page, args)
    futures = map(asyncio.ensure_future, tasks)

    return asyncio.gather(*futures, return_exceptions=True)


async def load_page(url, session, timeout=3):
    try:
        async with session.get(url, timeout=timeout) as response:
            text = await response.text()
            print(len(text))
    except Exception:
        logging.warning(type(e))


if __name__ == '__main__':
    loop = asyncio.get_event_loop()

    conn = aiohttp.TCPConnector(limit=1)
    session = aiohttp.ClientSession(connector=conn, loop=loop)
    loop.run_until_complete(main(session))
    loop.close()