1

When I reference another endpoint inside an async function in my FastAPI script, that endpoint will hang and fail to load. Despite http://127.0.0.1:8000/data?nums=1&nums=2&nums=3 returning the correct url (can test by changing the return statement to return url), the request hangs and also does not output anything to the terminal for debugging. How can I take parameter data and feed those values into another endpoint in FastAPI? Thanks!

from fastapi import FastAPI, Query
from typing import List
import requests

app = FastAPI()

@app.get('/data')
async def test_data(nums: List[int] = Query(...)):
    return [nums[i] * 2 for i in range(len(nums))]

@app.get('/load_data')
async def load_data(nums: List[int] = Query(...)):
    url = 'http://127.0.0.1:8000/data?'
    for i in range(len(nums)):
        url += f'nums={nums[i]}&'
    return requests.get(url).json()

# uvicorn reprex:app --reload
# http://127.0.0.1:8000/data?nums=1&nums=2&nums=3
# this will result in a hung request call
Chris
  • 18,724
  • 6
  • 46
  • 80
codeweird
  • 145
  • 3
  • 11
  • 2
    I don’t know much about FastAPI, but requests is blocking the event loop from yielding to any other tasks. You should use an asyncio-aware library like aiohttp or httpx to make requests asynchronously. There’s also probably a better way of calling internal endpoints than using an http client. – dirn Aug 25 '22 at 02:11
  • If you need the same data in two different endpoints it'd be better to factor out that functionality to a function and call it from both endpoints. – Jesse Aug 25 '22 at 02:38
  • @Jesse, so that's what I have in my production test. Essentially I have the 'computation' defined as a normal function (no `async`) and then call that to my first api endpoint -- which *does* server a purpose aside from being needed by the second endpoint. Then I want the second endpoint to call the results from the first given some parameters, and those data are used for a separate calculation inside the second endpoint. – codeweird Aug 25 '22 at 02:54
  • 2
    But why not refactor out the common part from the first API endpoint to a common function and call that instead? What's the reason for doing an HTTP request at all, even if internally? It's just unnecessary overehead. – MatsLindh Aug 25 '22 at 08:03
  • Does this answer your question? [FastAPI runs api-calls in serial instead of parallel fashion](https://stackoverflow.com/questions/71516140/fastapi-runs-api-calls-in-serial-instead-of-parallel-fashion) – Chris Dec 22 '22 at 11:29
  • 1
    In addition to [this answer](https://stackoverflow.com/a/71517830/17865804) I wouldn't suggest using Python `requests` module in an `async` environment, but rather use the `AsyncClient` from [`httpx`](https://www.python-httpx.org/async/) , as demonstrated [here](https://stackoverflow.com/a/73736138/17865804), as well as [here](https://stackoverflow.com/a/74239367/17865804) and [here](https://stackoverflow.com/a/74239367/17865804). – Chris Dec 22 '22 at 11:34
  • Does this answer your question? [How can I send an HTTP request from my FastAPI app to another site (API)?](https://stackoverflow.com/questions/63872924/how-can-i-send-an-http-request-from-my-fastapi-app-to-another-site-api) – Gino Mempin Dec 23 '22 at 10:37
  • faced the same problem, remove the async part and it would work just fine – Rukaiya Fahmida Dec 22 '22 at 10:27

0 Answers0