Python learning Asynchronous [Asyncio +aiohttp module] for---IO

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.