Aiohttp
aiohttp is a module that supports HTTP requests based on the Asyncio module, which is more comprehensive than 8.4.2 " based on the Asyncio implementation of TCP analog HTTP requests "
Install Aiohttp:
PIP3 Install Aiohttp
Asyncio + aiohttp Implementing an asynchronous request "problematic"
Import aiohttpimport asyncio@asyncio.coroutinedef fetch_async (URL): print (URL) response = yield from Aiohttp.request (' GET ', url) # data = yield from Response.read () # Print (URL, data) print (URL, response) response.close () tasks = [Fetch_async (' http://www.cnblogs.com/'), Fetch_async (' http://www.chouti.com/')]event _loop = Asyncio.get_event_loop () results = Event_loop.run_until_complete (Asyncio.gather (*tasks)) Event_loop.close ()
Asyncio + requests complete IO async
Asyncio + requests Complete IO async
Import Asyncioimport requests@asyncio.coroutinedef Fetch_async (func, *args): # Get Event loop: There is a loop waiting for this user's response loop = Asyncio.get_event_loop () Future = Loop.run_in_executor (None, Func, *args) # Executes the Get function passed in response = yield from Future print (Response.url, response.content) tasks = [ fetch_async (Requests.get, ' http://www.cnblogs.com/ ftl1012/'), fetch_async (requests.get, ' http://dig.chouti.com/images/homepage_download.png ')]loop = Asyncio.get_event_loop () results = Loop.run_until_complete (Asyncio.gather (*tasks)) Loop.close ()
Python learning Asynchronous [Asyncio +aiohttp module] for---IO