Need Assistance?

support@tutorialshub.in

Concurrency and Performance

Multiprocessing

ADVERTISEMENT
Python Free Lesson
5 min read
ADVERTISEMENT

Asynchronous code cooperates through an event loop

asyncio provides an event loop for cooperative concurrency. An async def function creates a coroutine, and await allows other scheduled work to run while an asynchronous operation is waiting.

Async does not make blocking code disappear

If an asynchronous application calls a slow blocking function directly, the event loop can stop making progress. Use asynchronous libraries for asynchronous workloads or deliberately move blocking work elsewhere.

Gather independent tasks

When several network operations can run independently, scheduling them together can reduce total waiting time. The benefit comes from overlapping waiting, not from making the CPU execute Python bytecode magically faster.

Practice: run three asynchronous tasks with different delays using asyncio.gather() and compare the elapsed time with sequential execution.

ADVERTISEMENT