Created
March 9, 2018 18:17
-
-
Save ansig/d73c8e027721b1f4fedfa2d7fd2bef3a to your computer and use it in GitHub Desktop.
Recursively make get requests to a certain depth
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
# -*- coding: utf-8 -*- | |
import requests | |
import timeit | |
from multiprocessing import Pool | |
from functools import partial | |
def get_result(url, depth=1): | |
r = requests.get(url) | |
result = [r.status_code] | |
if depth > 1: | |
next_depth = depth - 1 | |
next_result = get_result(url, depth=next_depth) | |
result.append(next_result) | |
return result | |
def main(): | |
urls = [ | |
'http://www.python.org', | |
'http://www.python.org/about/', | |
'http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html', | |
'http://www.python.org/doc/', | |
'http://www.python.org/download/', | |
'http://www.python.org/getit/', | |
'http://www.python.org/community/', | |
'https://wiki.python.org/moin/', | |
'http://planet.python.org/', | |
'https://wiki.python.org/moin/LocalUserGroups', | |
'http://www.python.org/psf/', | |
'http://docs.python.org/devguide/', | |
'http://www.python.org/community/awards/' | |
] | |
print("Working...") | |
pool = Pool(4) | |
result = pool.map(partial(get_result, depth = 4), urls) | |
pool.close() | |
pool.join() | |
print("Done!") | |
for r in result: | |
print(r) | |
if __name__ == '__main__': | |
time = timeit.timeit("main()", setup="from __main__ import main", number=1) | |
print("Time: %s" % time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment