Threads vs. Async Threads vs. Async python python

Threads vs. Async


  1. It is very difficult to write code that is thread safe. With asyncronous code, you know exactly where the code will shift from one task to the next and race conditions are therefore much harder to come by.
  2. Threads consume a fair amount of data since each thread needs to have its own stack. With async code, all the code shares the same stack and the stack is kept small due to continuously unwinding the stack between tasks.
  3. Threads are OS structures and are therefore more memory for the platform to support. There is no such problem with asynchronous tasks.


There are two ways to create threads:

synchronous threading - the parent creates one (or more) child threads and then must wait for each child to terminate. Synchronous threading is often referred to as the fork-join model.

asynchronous threading - the parent and child run concurrently/independently of one another. Multithreaded servers typically follow this model.

resource - http://www.amazon.com/Operating-System-Concepts-Abraham-Silberschatz/dp/0470128720


  1. Assume you have 2 tasks, which does not involve any IO (on multiprocessor machine).In this case threads outperform Async. Because Async like a single threaded program executes your tasks in order. But threads can execute both the tasks simultaneously.

  2. Assume you have 2 tasks, which involve IO (on multiprocessor machine).In this case both Async and Threads performs more or less same (performance might vary based on number of cores, scheduling, how much process intensive the task etc.). Also Async takes less amount of resources, low overhead andless complex to program over multi threaded program.

How it works? Thread 1 executes Task 1, since it is waiting for IO, it is moved to IO waiting Queue. Similarly Thread 2 executes Task 2, since it is also involves IO, it is moved to IO waiting Queue. As soon as it's IO request is resolved it is moved to ready queue so the scheduler can schedule the thread for execution.

Async executes Task 1 and without waiting for it's IO to complete it continues with Task 2 then it waits for IO of both the task to complete. It completes the tasks in the order of IO completion.

Async best suited for tasks which involve Web service calls, Database query calls etc., Threads for process intensive tasks.

The below video explains aboutAsync vs Threaded model and also when to use etc., https://www.youtube.com/watch?v=kdzL3r-yJZY

Hope this is helpful.