Blocking vs. Non-Blocking IO

Resources

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.