Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib
- import re
- import timeit
- from multiprocessing import Pool
- def get_url_content(url):
- return urllib.urlopen(url).read()
- def get_links_from_content(content):
- return re.findall(r'href=\"(http.+?)\"', content)
- def get_links_from_url(url):
- return get_links_from_content(get_url_content(url))
- if __name__ == '__main__':
- urls = [
- "http://google.com",
- "http://ukr.net",
- "http://i.ua",
- "http://mail.ru",
- "http://yahoo.com",
- "http://habrahabr.ru",
- ]
- """Do without threads"""
- def without_threads():
- list(map(get_links_from_url, urls))
- print(timeit.timeit(stmt=without_threads, number=1))
- """Do with threads"""
- p = Pool(8)
- def with_threads():
- list(p.map(get_links_from_url, urls))
- print(timeit.timeit(stmt=with_threads, number=1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement