Best Practice to Make HTTP Request in FastAPI Application

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ต.ค. 2024
  • In this video, you'll learn how to make highly available FastAPI endpoints using asynchronous HTTP requests.
    We'll cover:
    Why standard requests library blocks the event loop
    Introducing httpx for asynchronous requests in FastAPI
    3 Best Practices:
    Creating an AsyncClient with context manager (basic approach)
    Using a dependency for cleaner code
    Application-wide AsyncClient for efficiency (with trade-offs)
    Optimize your FastAPI application for maximum responsiveness!
    Hit that subscribe button for more FastAPI tips and tricks!
    Search Queries:
    fastapi asynchronous http requests
    improve fastapi performance
    fastapi high availability
    fastapi async client
    fastapi requests library blocking event loop
    httpx in fastapi
    fastapi async client best practices
    fastapi dependency for async client
    fastapi application state async client
    #fastapi
    #python
    #webdev
    #backend
    #api
    #programming
    #fastapi
    #python
    #webdev
    #backend
    #api
    #programming

ความคิดเห็น • 21

  • @nitishvirtual4745
    @nitishvirtual4745 4 หลายเดือนก่อน +1

    Great video. Short and to the point. Subscribed.

  • @apefu
    @apefu 2 หลายเดือนก่อน

    I have so many questions, I am so sorry.
    Should you pass app to the AsyncClient() as well for the ASGI server?
    Also, how would the last example affect APIRouters defined in separate files? How would they reach app.client?
    They could have their separate lifespan function but wouldn't it be prefered to use the same object?
    Also, I don't know, but wouldn't it be better to use app.state.client?
    Oh, and what about POST requests, would you still use the Request object to reach the client? It works, but it gets messy code wise.

  • @MakeDataUseful
    @MakeDataUseful 3 หลายเดือนก่อน

    Great video, thanks for sharing

  • @pritamsarkar3371
    @pritamsarkar3371 5 หลายเดือนก่อน +2

    dont we have to enable the 68 no line? "app = Fastapi(lifespan=lifespan)" ?

    • @codecollider
      @codecollider  5 หลายเดือนก่อน +3

      Yes you have to uncomment that line if you are using lifespan, forget to tell in the video.

  • @temirlllan
    @temirlllan 5 หลายเดือนก่อน

    thx bro! so useful material, keep going

  • @MathClubfor6789
    @MathClubfor6789 4 หลายเดือนก่อน

    Great knowledgable video

  • @godgiven994
    @godgiven994 6 หลายเดือนก่อน

    Hello! Thank you for the video! Could you tell me how to measure the request (in your video it’s about 300ms)? Using a decorator? If so, I tried it, but it didn’t help, because time information is simply not displayed in the logs. I would like this information to be absorbed and for me to see a clear difference in the use of reguess and nttpx. I can’t leave a comment with a link to pastebin because youtube deletes such comments

    • @codecollider
      @codecollider  6 หลายเดือนก่อน +3

      I mentioned 300 milliseconds (ms) for external API calls, but that's a general idea based on what I've seen. The actual time can vary.
      No matter if you use requests or httpx, the external API itself will take some time (usually hundreds of milliseconds) to respond. We can't control that wait time.
      Focus on not blocking your app: The key difference is how httpx.AsyncClient handles waiting. With requests, your application grinds to a halt while it waits for the API response. This can be bad if your app needs to do other things.
      httpx.AsyncClient lets your application keep working even while waiting for the API. This is because it uses an asynchronous approach, which means it can juggle multiple tasks at once.

    • @godgiven994
      @godgiven994 6 หลายเดือนก่อน

      @@codecollider Thank you for answer!

    • @muhammadhassankiyani8953
      @muhammadhassankiyani8953 5 หลายเดือนก่อน

      @@codecollider How is it different from multi-threading then?

    • @dixon1e
      @dixon1e 4 หลายเดือนก่อน

      Look at using a middleware to measure the elapsed time of a request. It always frightened me to even think about using middleware, probably due to CORS headaches, but when I finally did this exact use case, it turned out to be dirt simple.

    • @Sam-rb1id
      @Sam-rb1id 4 หลายเดือนก่อน

      @@dixon1e Did it make any difference to your run time though. With this simple example I don't think it would make any difference because it doesnt have any other task to execute while waiting for the response?

  • @thampasaurusrex3716
    @thampasaurusrex3716 5 หลายเดือนก่อน

    thank you!

  • @amodsahabandu
    @amodsahabandu 4 หลายเดือนก่อน

    Thanks. ❤

  • @rohit1kumar
    @rohit1kumar 2 หลายเดือนก่อน

    got any benchmarks?

  • @oktay9784
    @oktay9784 4 หลายเดือนก่อน +4

    this is not about FastApi, its about sync functions and async functions so just dont use sync functions in async functions if sync one is takes a lot of time.

    • @MakeDataUseful
      @MakeDataUseful 3 หลายเดือนก่อน

      This is a fastAPI video

    • @805bluebell
      @805bluebell 2 หลายเดือนก่อน

      It's actually both. End of argument

  • @badrakhariunchimeg1031
    @badrakhariunchimeg1031 3 หลายเดือนก่อน +1

    Well fastapi ( fun get ( ))

  • @cyyan1139
    @cyyan1139 5 หลายเดือนก่อน

    thank you!