The requests Library is a good HTTP library for Python, and it can be very simple to perform various operations of HTTP, such as GET, post, and so on. However, the network request that the library executes is synchronous, that is, after the CPU sends the request instruction, the IO performs the operation such as send and wait, during this IO execution time, the CPU does nothing, so the CPU computing power is wasted. So, you can try to modify the network request to asynchronous, that is, during the time the IO is functioning, the CPU goes to do other things in this program, and so IO receives the response data, the CPU returns processing. Accidentally found the following post special share with everyone, and compared with the requests library for a simple response rate:
It is recommended to read the original blogger article first:
thank Yumbo the spirit of sharing, transfer to the original blog address: HTTPS://WWW.LETIANTIAN.ME/2014-10-04-ASYNCHRONOUS-REQUESTS/
-----------------------------------Gorgeous split-line-----------------------------------------------------------------------
The following is a comparison of the responses to the Grequests Library and the requests library
#-*-Coding:utf-8-*-
ImportGrequests
ImportTime
ImportRequests
ImportSys
Reload(SYS)
Sys.setdefaultencoding (' Utf-8 ')
# Compare results with network environment
now =int(Time.time ())
Timearray = Time.localtime (now)
Otherstyletime = Time.strftime ("%y--%m--%d%h:%m:%s", Timearray)
PrintOtherstyletime
URLs = [' http://letiantian.me/'] *10
#-------------------grequests Library Request-------------------------------------
# reqs = [Grequests.get (URL) for URL in URLs] # spents 1 seconds or so
# response = Grequests.map (reqs)
# Print Response[9].status_code
#-------------------Requests Library Request-------------------------------------
Reqs = [Requests.get (URL) forUrlinchURLs# spents 7 seconds
Printreqs[9].status_code
Now2 =int(Time.time ())
TimeArray2 = Time.localtime (now2)
OtherStyleTime2 = Time.strftime ("%y--%m--%d%h:%m:%s", TIMEARRAY2)
PrintOtherstyletime,'-------------',OtherStyleTime2
Pasting
Application of the asynchronous request Python library grequests and comparison with the response speed of the requests library