Created
July 1, 2022 13:16
-
-
Save amrakm/7b40c3820f35b0bf02e2d811c32ccb3b to your computer and use it in GitHub Desktop.
download url parallel
This file contains 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 eventlet,sys | |
# note: this urllib import doesn't work in Python2 | |
from eventlet.green.urllib.request import urlopen | |
file = sys.argv[1] # list of urls | |
with open(file,'r') as f: | |
urls = [x.rstrip() for x in f.readlines()] | |
urls = urls + urls | |
urls = urls + urls | |
urls = urls + urls | |
def fetch(url): | |
try: | |
return urlopen(url).read() | |
except: | |
return [1] | |
pool = eventlet.GreenPool(8) | |
for body in pool.imap(fetch, urls): | |
print(f"got body {len(body)}") | |
print(len(urls)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment