async def main(): async with aiohttp.ClientSession() as session: fetchers = [ fetch_status(session, 'https://www.example.com', 1), fetch_status(session, 'https://www.example.com', 1), fetch_status(session, 'https://www.example.com', 10) ] for finished_task in asyncio.as_completed(fetchers): print(await finished_task)
async def main(): async with aiohttp.ClientSession() as session: api_a = fetch_status(session, 'https://www.example.com') api_b = fetch_status(session, 'https://www.example.com', delay=2) done, pending = await asyncio.wait([api_a, api_b], timeout=1) for task in pending: if task is api_b: print('API B too slow, cancelling') task.cancel()
async def main(): async with aiohttp.ClientSession() as session: fetchers = \ [asyncio.create_task(fetch_status(session, 'https://example.com')), asyncio.create_task(fetch_status(session, 'https://example.com'))] done, pending = await asyncio.wait(fetchers) print(f'Done task count: {len(done)}') print(f'Pending task count: {len(pending)}') for done_task in done: result = await done_task print(result)
async def main(): async with aiohttp.ClientSession() as session: fetchers = [fetch_status(session, 'https://example.com', 1), fetch_status(session, 'https://example.com', 10), fetch_status(session, 'https://example.com', 10)] for done_task in asyncio.as_completed(fetchers, timeout=2): try: result = await done_task print(result) except asyncio.TimeoutError: print('We got a timeout error!') for task in asyncio.tasks.all_tasks(): print(task)
async def main(): async with aiohttp.ClientSession() as session: example_url = 'https://example.com' fetchers = [ fetch_status(session, example_url), fetch_status(session, example_url), fetch_status(session, example_url, delay=3) ] done, pending = await asyncio.wait(fetchers, timeout=1) print(f'Done task count: {len(done)}') print(f'Pending task count: {len(pending)}') for done_task in done: result = await done_task print(result)
async def main(): async with aiohttp.ClientSession() as session: url = 'https://www.example.com' fetchers = [ asyncio.create_task(fetch_status(session, url)), asyncio.create_task(fetch_status(session, url)), asyncio.create_task(fetch_status(session, url)) ] done, pending = await asyncio.wait(fetchers, return_when=asyncio.FIRST_COMPLETED) print(f'Done task count: {len(done)}') print(f'Pending task count: {len(pending)}') for done_task in done: print(await done_task)
async def main(): async with aiohttp.ClientSession() as session: good_request = fetch_status(session, 'https://www.example.com') bad_request = fetch_status(session, 'python://bad') fetchers = [ asyncio.create_task(good_request), asyncio.create_task(bad_request) ] done, pending = await asyncio.wait(fetchers) print(f'Done task count: {len(done)}') print(f'Pending task count: {len(pending)}') for done_task in done: # result = await done_task will throw an exception if done_task.exception() is None: print(done_task.result()) else: logging.error("Request got an exception", exc_info=done_task.exception())
async def main(): async with aiohttp.ClientSession() as session: fetchers = \ [asyncio.create_task(fetch_status(session, 'python://bad.com')), asyncio.create_task(fetch_status(session, 'https://www.example.com', delay=3)), asyncio.create_task(fetch_status(session, 'https://www.example.com', delay=3))] done, pending = await asyncio.wait(fetchers, return_when=asyncio.FIRST_EXCEPTION) print(f'Done task count: {len(done)}') print(f'Pending task count: {len(pending)}') for done_task in done: if done_task.exception() is None: print(done_task.result()) else: logging.error("Request got an exception", exc_info=done_task.exception()) for pending_task in pending: pending_task.cancel()
async def main(): async with aiohttp.ClientSession() as session: urls = ['https://example.com' for _ in range(1000)] requests = [fetch_status(session, url) for url in urls] status_codes = await asyncio.gather(*requests) print(status_codes)