Application of the asynchronous request Python library grequests and comparison with the response speed of the requests library

Source: Internet
Author: User

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

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.