Created
January 14, 2019 04:12
-
-
Save ben7th/e0c297a5346b0c2e7f4ee47386929a8f to your computer and use it in GitHub Desktop.
并发异步请求(用于 FC 调度层)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import grequests | |
| import requests | |
| from timeit import default_timer as timer | |
| urls = [ "https://1246105.cn-shenzhen.fc.aliyuncs.com/2016-08-15/proxy/charlene-chatlog/api/bundle/" ] * 10 | |
| for idx, url in enumerate(urls): | |
| urls[idx] = urls[idx] + str(idx) + '.xls' | |
| def req_sync(): | |
| result = [] | |
| for url in urls: | |
| result.append(requests.get(url)) | |
| return result | |
| def req_async(): | |
| res = [grequests.get(url) for url in urls] | |
| result = grequests.map(res, size=100) | |
| return result | |
| start = timer() | |
| # print(req_sync()) | |
| print(req_async()) | |
| end = timer() | |
| print(end-start) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment