Python learns asynchronous---io [gevent+grequests module]

Source: Internet
Author: User

Installing the Gevent module

PIP3 Install Gevent

Gevent instances

Import geventimport requestsfrom gevent import monkey# socket send request will go into wait state, gevent changed this mechanism # socket.setblocking (False)  --After sending a request, it will not wait for the server to respond Monkey.patch_all ()  # Find the built-in socket and change it to gevent own things def fetch_async (method, URL, req_ Kwargs):    print (method, URL, Req_kwargs)    response = Requests.request (Method=method, Url=url, **req_kwargs) Print (Response.url, response.content) # ##### send Request # # # # # # # # # # # # # # # #    3 tasks [Spawn here] [3], [actual], the #gevent. Each task will execute the Fetch_async function    gevent.spawn (fetch_async, method= ' get ', url= ' https://www.python.org/', req_kwargs={}),    Gevent.spawn (Fetch_async, method= ' get ', url= ' https://www.yahoo.com/', req_kwargs={}),    gevent.spawn (fetch _async, method= ' get ', url= ' https://github.com/', req_kwargs={}),])

Gevent also supports the pool

##### Send request (the maximum number of threads in the pool control) ###### can also be understood to send a maximum of 2 requests before the end of 2 requests to send a third request from Gevent.pool import Poolpool = Pool (2)  # perform up to 2 co-programs , none means no limit gevent.joinall ([    pool.spawn (Fetch_async, method= ' get ', url= ' https://www.python.org/', req_kwargs= {}),    pool.spawn (fetch_async, method= ' get ', url= ' https://www.yahoo.com/', req_kwargs={}),    pool.spawn (fetch _async, method= ' get ', url= ' https://www.github.com/', req_kwargs={}),])
grequests

Installing Grequests

PIP3 Install Grequests

Grequests actually encapsulates the method inside the gevent and then implements the asynchronous IO with requests

Grequests = gevent + Request

Grequests.map () Internal implementation

Grequest instances

Import Grequests  # is actually requests + geventrequest_list = [    # Send GET request    grequests.get (' https://www.baidu.com/ ', timeout=10.001),    grequests.get (' https://www.taobao.com/'),    grequests.get (' https://hao.360.cn/')]# # # # # # Execute and get response List # # # # # # # # # # # # # #response_list = Grequests.map (request_list)  # actually internal loop execution gevent Internal Joinall () method print (Response_ List) # ##### executes and gets the response list (handles exceptions) ###### def exception_handler (Request, exception): # print (request,exception) #     print (" Request failed ") # response_list = Grequests.map (Request_list, Exception_handler=exception_handler) # print (Response_ List

Python learns asynchronous---io [gevent+grequests module]

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.