How to do it...

  1. First, we will create single_thread.py to give us a benchmark for comparison. For this example, we will be contacting a number of websites and calculating the time it takes to open a connection to all of them:

        import urllib.request        import urllib.error        import time        def single_thread_retrieval():            start_time = time.time()            urls = ["https://www.python.org",                     "https://www.google.com",                     "https://www.techdirt.com",                    "https://www.facebook.com",                    "https://www.ibm.com",                    "https://www.dell.com",                    "https://www.amd.com",                    "https://www.yahoo.com",                    "https://www.microsoft.com",                    "https://www.apache.org"]            try:                for url in urls:                    urllib.request.urlopen(url)            except urllib.error.HTTPError:                pass            return time.time() - start_time
    • As we will be contacting ...

Get Secret Recipes of the Python Ninja now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.