Skip to content

Instantly share code, notes, and snippets.

@JustinSDK
Created August 14, 2018 07:54
Show Gist options
  • Save JustinSDK/c803d60a6559006a2d58ea3ca866c3c8 to your computer and use it in GitHub Desktop.
Save JustinSDK/c803d60a6559006a2d58ea3ca866c3c8 to your computer and use it in GitHub Desktop.
download.py
from urllib.request import urlopen
def download(url, file):
with urlopen(url) as url, open(file, 'wb') as f:
f.write(url.read())
urls = [
'http://openhome.cc/Gossip/Encoding/',
'http://openhome.cc/Gossip/Scala/',
'http://openhome.cc/Gossip/JavaScript/',
'http://openhome.cc/Gossip/Python/'
]
filenames = [
'Encoding.html',
'Scala.html',
'JavaScript.html',
'Python.html'
]
for url, filename in zip(urls, filenames):
download(url, filename)
# threading
'''
import threading
from urllib.request import urlopen
def download(url, file):
with urlopen(url) as url, open(file, 'wb') as f:
f.write(url.read())
urls = [
'http://openhome.cc/Gossip/Encoding/',
'http://openhome.cc/Gossip/Scala/',
'http://openhome.cc/Gossip/JavaScript/',
'http://openhome.cc/Gossip/Python/'
]
filenames = [
'Encoding.html',
'Scala.html',
'JavaScript.html',
'Python.html'
]
for url, filename in zip(urls, filenames):
t = threading.Thread(target=download, args=(url, filename))
t.start()
'''
# concurrent.futures
'''
from urllib.request import urlopen
from concurrent.futures import ThreadPoolExecutor
def download(url, file):
with urlopen(url) as url, open(file, 'wb') as f:
f.write(url.read())
urls = [
'http://openhome.cc/Gossip/Encoding/',
'http://openhome.cc/Gossip/Scala/',
'http://openhome.cc/Gossip/JavaScript/',
'http://openhome.cc/Gossip/Python/'
]
filenames = [
'Encoding.html',
'Scala.html',
'JavaScript.html',
'Python.html'
]
with ThreadPoolExecutor(max_workers=4) as executor:
for url, filename in zip(urls, filenames):
executor.submit(download, url, filename)
'''
# asyncio
'''
from urllib.request import urlopen
import asyncio
async def download(url, file):
with urlopen(url) as url, open(file, 'wb') as f:
f.write(url.read())
urls = [
'http://openhome.cc/Gossip/Encoding/',
'http://openhome.cc/Gossip/Scala/',
'http://openhome.cc/Gossip/JavaScript/',
'http://openhome.cc/Gossip/Python/'
]
filenames = [
'Encoding.html',
'Scala.html',
'JavaScript.html',
'Python.html'
]
loop = asyncio.get_event_loop()
tasks = [loop.create_task(download(url, filename)) for url, filename in zip(urls, filenames)]
loop.run_until_complete(asyncio.wait(tasks))
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment