summaryrefslogtreecommitdiff
path: root/python2/crawl.py
blob: cf4a218fbe3dc897b6aef5eaba4522096dad2a6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Compare the speed of downloading URLs sequentially vs. using futures."""

import datetime
import functools
import futures.thread
import time
import timeit
import urllib2

URLS = ['http://www.google.com/',
        'http://www.apple.com/',
        'http://www.ibm.com',
        'http://www.thisurlprobablydoesnotexist.com',
        'http://www.slashdot.org/',
        'http://www.python.org/',
        'http://www.bing.com/',
        'http://www.facebook.com/',
        'http://www.yahoo.com/',
        'http://www.youtube.com/',
        'http://www.blogger.com/']

def load_url(url):
    return urllib2.urlopen(url).read()

def download_urls_sequential(urls):
    url_to_content = {}
    for url in urls:
        try:
            url_to_content[url] = load_url(url)
        except:
            pass
    return url_to_content

def download_urls_with_executor(urls, executor):
    try:
        url_to_content = {}
        fs = executor.run_to_futures(
                (functools.partial(load_url, url) for url in urls))
        for future in fs.successful_futures():
            url = urls[future.index]
            url_to_content[url] = future.result()
        return url_to_content
    finally:
        executor.shutdown()

def main():
    for name, fn in [('sequential',
                      functools.partial(download_urls_sequential, URLS)),
                     ('threads',
                      functools.partial(download_urls_with_executor,
                                        URLS,
                                        futures.ThreadPoolExecutor(10))),
                     ('processes',
                      functools.partial(download_urls_with_executor,
                                        URLS,
                                        futures.ThreadPoolExecutor(10)))]:
        print '%s: ' % name.ljust(12),
        start = time.time()
        url_map = fn()
        print '%.2f seconds (%d of %d downloaded)' % (time.time() - start,
                                                     len(url_map),
                                                     len(URLS))

main()