Reputation: 5148
I have 4 APIs which I want to run in parallel on an AWS box with 16GB RAM and 8 CPUs. Can I run those APIs in parallel using python's Flask?
Basically, I want one of the APIs to take in user input and then query 3 other APIs in parallel.
I am new to the paradigm of APIs and parallel processing so any sort of help will be highly appreciated.
Here is what I am thinking:
import requests
r1 = requests.get(url_of_api1, data).json()
r2 = requests.get(url_of_api2, data).json()
r3 = requests.get(url_of_api3, data).json()
I want r1, r2 and r3 to be queried in parallel and not one after the other. How can this be done?
Upvotes: 0
Views: 963
Reputation: 14853
This could be the parallel code using threads.
import requests
import concurrent.futures
t = concurrent.futures.ThreadPoolExecutor(3) # 3 is the number of threads
get_json_from_url = lambda url: requests.get(url, data).json()
r1, r2, r3 = t.map(get_json_from_url, [url_of_api1, url_of_api2, url_of_api3])
Upvotes: 1