JS Promise
Python Coroutine Usages
Async task
import aiohttp import asyncio import timeit @asyncio.coroutine def fetch(url): print('Start', url) req = yield from aiohttp.request('GET', url) print('Done', url) @asyncio.coroutine def fetch_all(urls): fetches = [asyncio.Task(fetch(url)) for url in urls] yield from asyncio.gather(*fetches)
after python 3.7
import aiohttp import asyncio import timeit async def fetch(url): print('Start', url) req = yield from aiohttp.request('GET', url) print('Done', url) async def fetch_all(urls): fetches = [asyncio.Task(fetch(url)) for url in urls] yield from asyncio.gather(*fetches)