February 2019
Intermediate to advanced
672 pages
16h 50m
English
To apply concurrency, we simply use the threading module that we have been discussing to create separate threads to handle different web requests. Let's take a look at the Chapter12/example3.py file, as shown in the following code:
# Chapter12/example3.pyimport threadingimport requestsimport timedef ping(url): res = requests.get(url) print(f'{url}: {res.text}')urls = [ 'http://httpstat.us/200', 'http://httpstat.us/400', 'http://httpstat.us/404', 'http://httpstat.us/408', 'http://httpstat.us/500', 'http://httpstat.us/524']start = time.time()for url in urls: ping(url)print(f'Sequential: {time.time() - start : .2f} seconds')print()start = time.time()threads = []for url in urls: thread = threading.Thread(target=ping, ...Read now
Unlock full access