Jake
Jake

Reputation: 2912

FastAPI: Optimal way of sending multiple requests from an API

I have to serve an API that sends multiple requests to other APIs and merge their responses as the output.

What will be the most optimal way to do that? Is this some async await scenario? Appreciate the advice.

from fastapi import FastAPI
import requests

app = FastAPI()

@app.post('/api')
def main_api():
    JScontent = json.loads(request.json())
    input = JScontent['content']
    response1 = requests.post(url1, json={"input":input})
    response2 = requests.post(url2, json={"input":input})
    response3 = requests.post(url3, json={"input":input})
    response4 = requests.post(url4, json={"input":input})

    prediction = fuse_responses(response1, response2, response3, response4)
    return prediction

I am currently using Flask for development, but figured it might not have the capability, or will be a hassle to manage such a scenario, hence, open to changing to FastAPI.

[UPDATE]

Did some digging came across this link. I guess it can be applied similarly to FastAPI? I have no prior experience in async & await so appreciate the validation.

from fastapi import FastAPI


app = FastAPI()

async def send_requests(url, input_):
    res = await app.post(url, input_)
    return res
    

@app.post('/api')
async def main_api():
    JScontent = json.loads(request.json())
    input_ = JScontent['content']

    res1 = await send_requests(url1, input_)
    res2 = await send_requests(url2, input_)
    res3 = await send_requests(url3, input_)
    res4 = await send_requests(url4, input_)

    prediction = fuse_responses(res1, res2, res3, res4)
    return prediction

Upvotes: 6

Views: 19176

Answers (2)

Ravindra Babu Alla
Ravindra Babu Alla

Reputation: 71

The aggregation of multiple microservice calls can be done by the aggregation pattern mentioned above in both frameworks.

FAST API is a high-performing, asynchronous, non-blocking framework to build API's This is similar to the node framework in the Javascript full-stack world. You can definitely use async callbacks on each of the API calls and aggregate them once you have the results. FAST API is blazingly fast compared to FLASK. If you need a high performant system, FAST API might be a good job. Having said that, always remember that your API will be as performant as the lowest denominator within the API chain. Look at the documentation : https://fastapi.tiangolo.com/advanced/openapi-callbacks/ .

Flask is a reliable, time-tested, synchronous API framework with python. Mulitple enterprises have used flask to build the API's. All the code is written will be executed sequentially unless explicit thread management is used.

https://creativedata.stream/multi-threading-api-requests-in-python/

Do reply if you need additional help or have questions.

Upvotes: 3

lsabi
lsabi

Reputation: 4456

Yes, using the asynchronous functionality is the correct way of dealing with such kind of problems.

What you're trying to achieve, though, is called AGGREGATOR PATTERN, where one service receives the request and calls all the different services. It is frequent in the case of microserves, where a page contains information that comes from multiple microservices. It is sometimes implemented in the gateway (a.k.a. API AGGREGATION PATTERN), but most of the time I see this pattern in a dedicated service.

Below a couple of links that may be useful:

https://learn.microsoft.com/en-us/azure/architecture/patterns/gateway-aggregation

And here, which uses asynchronous code to achieve what you are trying to achive

https://dzone.com/articles/microservices-aggregator-design-pattern-using-aws

NOTE You can always spawn new threads from the flask app to carry out the requests. Alternatively, you can always create an asynchronous function that will call all the URLs asynchronously, wait for their responses, aggregate them and return the result.

Here's a short tutorial on how you can achieve your goal running asynchronous code from synchronous code.

https://realpython.com/async-io-python/

Upvotes: 1

Related Questions