Created
April 1, 2020 21:02
-
-
Save marcoaaguiar/6752ea1f7285268fa07d52666712112f to your computer and use it in GitHub Desktop.
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 threading | |
from contextlib import suppress | |
from functools import partial | |
from urllib.error import URLError | |
from urllib.request import urlopen | |
from first_example import timeit | |
links = [ | |
"http://www.google.com/", | |
"http://www.youtube.com/", | |
"http://www.apple.com/", | |
"http://www.linkedin.com/", | |
"http://www.microsoft.com/", | |
"http://www.blogger.com/", | |
"http://www.support.google.com/", | |
"http://www.play.google.com/", | |
"http://www.cloudflare.com/", | |
"http://plus.google.com/", | |
"http://en.wikipedia.org/", | |
"http://maps.google.com/", | |
"http://wordpress.org/", | |
"http://docs.google.com/", | |
] | |
def task(link): | |
with suppress(URLError): | |
contents = urlopen(link).read() | |
def run_tasks_synchronous(): | |
for link in links: | |
task(link) | |
def run_tasks_threaded(): | |
threads = [] | |
# Create and start threads | |
for link in links: | |
t = threading.Thread(target=partial(task, link), daemon=True) | |
t.start() | |
threads.append(t) | |
# Wait all to finish | |
for t in threads: | |
t.join() | |
timeit(run_tasks_synchronous) | |
timeit(run_tasks_threaded) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment