Blocking vs. Non-Blocking IO
Resources
- https://medium.com/coderscorner/tale-of-client-server-and-socket-a6ef54a74763
- https://www.geeksforgeeks.org/blocking-and-nonblocking-io-in-operating-system/
Blocking IO:
- Waits for operation to complete before returning control
- Simple to understand and program
- Can lead to inefficient resource use if not managed properly
# Blocking IO example
data = file.read() # The thread will wait here until the read operation is complete.
process(data) # This line will only execute after the file.read() operation has completed.
Non-blocking IO:
- Returns control immediately, operation continues in the background
- More complex to program due to need for callbacks or polling
- Enables efficient resource use, especially in high-load scenarios
# Non-blocking IO example
file.read(callback=process) # Initiates a read operation and immediately returns control to the next line.
# Other code can execute now, while `file.read` is still ongoing.