How FastAPI Handles Requests Behind the Scenes

  Рет қаралды 38,484

Code Collider

Code Collider

Күн бұрын

Пікірлер: 64
@moneeshkumar1838
@moneeshkumar1838 8 ай бұрын
Great content brother Quick Modification: sync router is Concurrency not Parallelism. In python parallelism is achieved only by multiprocessing
@vladhaidukkk-learning
@vladhaidukkk-learning 8 ай бұрын
Great video, but I think it’s important to mention that multi-threading in Python is not parallel
@bakasenpaidesu
@bakasenpaidesu 7 ай бұрын
Multi processing*
@vladhaidukkk-learning
@vladhaidukkk-learning 7 ай бұрын
​@@bakasenpaidesu Actually you are wrong; multiprocessing is parallel because Python spawns an entirely new process with an entirely new interpreter.
@benshapiro9731
@benshapiro9731 6 ай бұрын
Multi threading in python is technically also parallel programming whenever a thread releases the GIL, such as during time.sleep or open calls. In those specific instances, there can be two (or more) threads truly executing in parallel in the same python process, because one thread is waiting for the results of a system call, during which time it releases the GIL since reference counts don’t need to be updated, allowing another thread to acquire the GIL and execute a piece of code in parallel.
@benshapiro9731
@benshapiro9731 6 ай бұрын
On this topic also: check out the beta of. Python 3.13! There is a flag that can be passed when launching python that removes the GIL, allowing truly parallelized execution with just threads. Been playing around with asyncio and concurrent.futures.ThreadPoolExecutor -> noticeable speed up. Shame that c-extension based libraries like numpy are unusable with this setting
@vladhaidukkk-learning
@vladhaidukkk-learning 6 ай бұрын
​@@benshapiro9731 This is called concurrency, not parallelism. Parallelism is when two or more tasks can run simultaneously, using the CPU, without waiting for I/O-bound operations. While the GIL doesn't prevent Python from switching context between threads waiting for I/O-bound operations, this is still considered concurrency, not parallelism.
@AnaskiBaithak
@AnaskiBaithak 9 ай бұрын
Thank you for this, I always wondered the difference between async def and def
@usama57926
@usama57926 Ай бұрын
best video on fastapi, straight to the point ❤
@PriyankRupareliya
@PriyankRupareliya 3 ай бұрын
This is very valuable content. New job, starting to work on FastAPI. The whole python paradigm is new for me. This video helped clear one of the most critical concept for me. Thanks brother.
@hardikvegad3508
@hardikvegad3508 6 күн бұрын
What an excellent explanation. Thank you!
@saeed-khan-cs
@saeed-khan-cs Ай бұрын
Precisely on point video! I am new to fastapi & I was looking for exactly this information. Kudos!
@clearthinking5441
@clearthinking5441 Ай бұрын
Can all blocking operations be made non-blocking with asyncio? You advice is to use def for blocking operations, I am wondering wether it would be better advice to ensure that all operations are non-blocking and use async def always ...?
@codecollider
@codecollider Ай бұрын
You're absolutely right that using only non-blocking operations is ideal, and relying on async def wherever possible is great advice. However, there are cases where blocking operations can't be avoided like when an async version of a library you need doesn't exist, and you're stuck with a synchronous one. It's all about adapting to the tools available!
@sarvesh6785
@sarvesh6785 2 ай бұрын
Amazing Video brother. You solved my issue.
@joeygibli
@joeygibli 2 ай бұрын
It would be more clear if you wrote “Hello 1” and “Hello 2”; “Bye 1” and “Bye 2” for the different functions to see which routine the messages are coming from
@정하은-h6s
@정하은-h6s 8 ай бұрын
OMG! This is so helpful and a great video. Thank you and please post more videos like this!
@vikranttyagiRN
@vikranttyagiRN 6 ай бұрын
Nice explanation. Concise and to the point.
@sany2k8
@sany2k8 6 ай бұрын
Great explanation, you should create more videos bro...
@valentinmilanesio02
@valentinmilanesio02 Ай бұрын
Great video! I have a question, in the first case, when the second request arrives inmediately, where would be the queue and who is handling that queue?
@codecollider
@codecollider Ай бұрын
The queue is managed by the ASGI server (like Uvicorn). When the second request arrives, it’s held in the server's queue until the event loop is free to process it.
@valentinmilanesio02
@valentinmilanesio02 Ай бұрын
@@codecollider thanks!!
@ishaquenizamani9800
@ishaquenizamani9800 7 ай бұрын
Thanks for clearing this concept.
@aguspray
@aguspray Ай бұрын
What if my endpoint have database process (post data) and blocking i/o for timeout process? what should i choose? Async def or normal def?
@usama57926
@usama57926 Ай бұрын
@@aguspray use async querying methods for db that way you can use async endpoint.
@aguspray
@aguspray Ай бұрын
@ inside of code, there is also blocking process (while loop), then how to make it asynchronously?
@PyPeak
@PyPeak 6 ай бұрын
Beautifully explained!
@sticksen
@sticksen 6 ай бұрын
My question would be how FastAPI then manages workload when it´s handed over to the worker thread. Because I can only see one worker thread running, at the same time it handles 40 'workloads' concurrently.
@Shane1994322
@Shane1994322 6 ай бұрын
Clearly explained!! Thank you
@hyakuheli
@hyakuheli 3 ай бұрын
what happens if the server is created with multiple processes (for multiple cores) with uvicorn main:app --workers 4. The first 4 clients are given a new process? or it can be either a process or another thread if a thread is in a non-blocking task? and what happens when there are more than 4 clients at the same time in this case? the new clients are assigned to threads randomly across the 4 processes?
@lwangacaleb2729
@lwangacaleb2729 7 ай бұрын
I need some help, I want to create a fast api endpoint that calls a synchronous function that has a lot of blocking I/0 operations. But I want the endpoint function to run asynchronously so it can accept many requests at the same time. How should I do this, is there an alternative approach?
@Praise-rs4mc
@Praise-rs4mc 7 ай бұрын
The only way to achieve that is to use multi-threading which I advice against.... instead, make the function asynchronous and try to find the non-blocking function for what you want to do...
@Praise-rs4mc
@Praise-rs4mc 7 ай бұрын
Better still, use the run_in_threadpool function from fastapi to run the process in a different thread so that you don't block the event...better than implementing multi threading on your own.
@lwangacaleb2729
@lwangacaleb2729 7 ай бұрын
@@Praise-rs4mc thanks alot, I will give it a try.
@fhuadbalogun2997
@fhuadbalogun2997 3 ай бұрын
Thank you very much for this.
@rainbowrunner1136
@rainbowrunner1136 4 ай бұрын
Hello, great video. I am super new to python. And started using very recently for backend where its being used along with FastAPI. Can you please tell me what are some concepts/ topics which I should be aware for developing efficient code. For eg from this video, I learnt that which function runs concurrently and which doesn't. I had no idea tbh about this before. If you could spare few minutes and share some stuff which I should know it would be great.
@thanhlongle6276
@thanhlongle6276 9 ай бұрын
Thanks man for the video. I am trying to use fast api for db CRUD, which one do you think i should use for get post put and delete?
@codecollider
@codecollider 9 ай бұрын
It depends on whether your database library supports non-blocking queries. Ideally, for endpoints involving database calls, use async def if your library allows awaiting query execution (like await db.execute()). If you're using SQLAlchemy, it provides both blocking and non-blocking methods for queries. It's generally recommended to use the non-blocking approach for better performance.
@thanhlongle6276
@thanhlongle6276 9 ай бұрын
@@codecollider thank you, everything i write is in normal, non async, and I am using sqlite3 package. I think i will use normal def for all of it, since they are all parallel, and the blocking of read and write on database is performed by SQLite itself
@JainamParekh-pk6sz
@JainamParekh-pk6sz 8 күн бұрын
Hey I actually tried the code and found that all works sequentially for me. I executed the same code which you wrote. could please give me reason ? Please explain.
@codecollider
@codecollider 8 күн бұрын
I guess you are testing on a single browser, if you look closely I had used two different browsers to make multiple requests. Try two browsers, hopefully that should resolve the issue.
@JainamParekh-pk6sz
@JainamParekh-pk6sz 8 күн бұрын
@@codecollider Okay thanks that helped.
@adityahpatel
@adityahpatel 3 ай бұрын
2 and 3 look the same. hello of each is printed first then bye from each. how is 2 and 3 different? its not explained crystal clearly
@ArulMuruganAS
@ArulMuruganAS 4 ай бұрын
its working as explained in unix but for some reason its a different behavior in windows
@arjunc5896
@arjunc5896 6 ай бұрын
def endpoint3() is not running parallely for me as supposed to what u said in the video. Instead it is sunning one at a time. Do u know why?
@codecollider
@codecollider 6 ай бұрын
I believe you are testing APIs in the browser. Sometimes, browsers like Chrome have limitations on making parallel requests to the same URL. In the video, if you look closely, I am using two different browsers to hit the same API in parallel. You can try the same approach.
@arjunc5896
@arjunc5896 6 ай бұрын
@@codecollider Yes you are right. I tried from different browsers and it worked. Strange though. Thanks
@myselfriz
@myselfriz 7 ай бұрын
very well explanation.
@rajeshwariraghuwanshi3852
@rajeshwariraghuwanshi3852 Ай бұрын
gem video ✨
@nishantthakre8174
@nishantthakre8174 7 күн бұрын
The content is great, but Python's multithreading can't achieve true parallelism because of GIL which is introduced for thread safety. However, there are ways to work around this, such as using multiprocessing or alternative compilers like Jython or IronPython, instead of the default CPython. Also the multithreaded system demonstrated in your video is only achieving perceived parallelism and not actual parallel execution.
@ChrisHalden007
@ChrisHalden007 7 ай бұрын
Great video. Thanks
@startup_cult
@startup_cult 4 ай бұрын
this is such an underrated video
@henilshah.
@henilshah. 2 ай бұрын
coooooooooooooooooooooooooooooooooolllllllllll 🤟thanks man
@gck2p3education90
@gck2p3education90 20 күн бұрын
Nice
@adrianmisak07
@adrianmisak07 9 ай бұрын
great video
@lucaspraciano4640
@lucaspraciano4640 8 ай бұрын
@adityahpatel
@adityahpatel 23 күн бұрын
It is extremeley easy to demonstrate thius using sleep. Every tutorial on the web uses sleep. the probolem is wvery funciton is not awaitable, hence this example doesnt apply.. in the real worldIt i
@robertavetisyan8282
@robertavetisyan8282 7 ай бұрын
boooozi txeq
@Rejuyan-Ahmed
@Rejuyan-Ahmed 21 күн бұрын
Thanks man
@udaym4204
@udaym4204 7 ай бұрын
can you make fastapi how run under the hood and how @app.exception_handler work Thanks awesome contentent
@ashwinabrahamjacob525
@ashwinabrahamjacob525 4 ай бұрын
great video
AsyncIO, await, and async - Concurrency in Python
9:12
Socratica
Рет қаралды 115 М.
Requests vs HTTPX vs Aiohttp
15:11
ArjanCodes
Рет қаралды 40 М.
人是不能做到吗?#火影忍者 #家人  #佐助
00:20
火影忍者一家
Рет қаралды 20 МЛН
Леон киллер и Оля Полякова 😹
00:42
Канал Смеха
Рет қаралды 4,7 МЛН
Quando A Diferença De Altura É Muito Grande 😲😂
00:12
Mari Maria
Рет қаралды 45 МЛН
FastAPI Internals - How does it work? - Marcelo Trylesinski
31:08
Python Italia
Рет қаралды 3,5 М.
Javascript Promises vs Async Await EXPLAINED (in 5 minutes)
5:50
Roberts Dev Talk
Рет қаралды 617 М.
Why You NEED To Learn FastAPI | Hands On Project
21:15
Travis Media
Рет қаралды 172 М.
Next-Level Concurrent Programming In Python With Asyncio
19:19
ArjanCodes
Рет қаралды 184 М.
How to Make 2500 HTTP Requests in 2 Seconds with Async & Await
4:27
John Watson Rooney
Рет қаралды 134 М.
APIs Explained (in 4 Minutes)
3:57
Exponent
Рет қаралды 1,1 МЛН
What is Database Sharding?
9:05
Anton Putra
Рет қаралды 68 М.
Concurrency Vs Parallelism!
4:13
ByteByteGo
Рет қаралды 100 М.
人是不能做到吗?#火影忍者 #家人  #佐助
00:20
火影忍者一家
Рет қаралды 20 МЛН