call api parallel in python code example
Example: call the api parallel in python
from concurrent.futures import ThreadPoolExecutor as PoolExecutor
import http.client
import socket
def get_it(url):
try:
connection = http.client.HTTPSConnection(url, timeout=2)
connection.request("GET", "/")
response = connection.getresponse()
return response.read()
except socket.timeout:
pass
urls = [
"www.google.com",
"www.youtube.com",
"www.wikipedia.org",
"www.reddit.com",
"www.httpbin.org"
] * 200
with PoolExecutor(max_workers=4) as executor:
for _ in executor.map(get_it, urls):
pass