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]