Skip to content

Instantly share code, notes, and snippets.

@hustshawn
Created May 6, 2017 07:10
Show Gist options
  • Save hustshawn/b7b9284f9a50fa6f93f2d5126867fa4a to your computer and use it in GitHub Desktop.
Save hustshawn/b7b9284f9a50fa6f93f2d5126867fa4a to your computer and use it in GitHub Desktop.
A demo to utilize asyncio to fire multiple requests
import requests
import asyncio
import time
urls = [
"www.baidu.com",
"www.sina.com",
"www.google.com.hk",
"www.sohu.com"
]
async def request(loop, url):
print("Sending request {}".format(url))
req_start = time.time()
r = await loop.run_in_executor(None, requests.get, url)
req_end = time.time()
print("After the yield {0} {1}. Elapsed: {2}".format(url, r.status_code, req_end - req_start))
loop = asyncio.get_event_loop()
tasks = [request(loop, 'http://' + url) for url in urls]
start_t = loop.time()
print("Event-Loop Time Start: {}".format(start_t))
loop.run_until_complete(asyncio.wait(tasks))
end_t = loop.time()
print("Event-Loop Time end: {}".format(end_t))
print("Event-Loop Time elapsed: {}".format(end_t - start_t))
loop.close()
@hustshawn
Copy link
Author

hustshawn commented May 6, 2017

Output sample as below:

Event-Loop Time Start: 11738.149305836
Sending request http://www.sohu.com
Sending request http://www.sina.com
Sending request http://www.baidu.com
Sending request http://www.google.com.hk
After the yield http://www.google.com.hk 200. Elapsed: 0.11449790000915527
After the yield http://www.baidu.com 200. Elapsed: 0.13787007331848145
After the yield http://www.sohu.com 200. Elapsed: 0.285675048828125
After the yield http://www.sina.com 200. Elapsed: 0.5604429244995117
Event-Loop Time end: 11738.711558289
Event-Loop Time elapsed: 0.5622524530008377

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment