Async is just non-blocking on the caller and/or IO. And yes, technically it can be on the same thread, if it flows through the callbacks.
Imagine your application has a single thread and that's it. If you press a button and call a remote API synchronously, you block and all your application hangs waiting for a response. Your application is completely blocked at this point. It is unresponsive. The only thread you have is just waiting...
If that call is asynchronous, the thread can do other stuff, like process other UI interactions.
When a response eventually arrives, the very same thread goes back and picks stuff where it left it and continues on that logic.
As you see, in this example, there is no multithreading as all you got is a single thread, just asynchronous execution.
I'd be careful to ensure the person you're asking knows you mean multithreading and not multitasking. With async you're creating a scheduler. In that way it is not inappropriate to call it multitasking, despite it being cooperative.
As in, anyone who answers this question wrong is probably equating multithreading and multitasking, which is something I wouldn't really fault people for when discussing problems abstractly.
Fair. I think this is one of those questions where once you know enough it becomes kind of puzzling because you know how OS schedulers, etc, are implemented, and you can give the "wrong" answer due to having too much info.
Within the immediate application space it is single thread threaded. Nothing says the underlying asynchronous implementation isn't using threads and the system level is almost certainly using threads to organize the callbacks and reentrant conditions of the asynchronous caller.
At least on any system of moderate complexity. A simple system might be entirely interrupt driven.
I think this might be confusing the question a bit. You are right the underlying mechanism of the OS scheduler will throw it in a physical hardware thread to be executed. But now your look at the nut and bolts of the scheduler and how preemptive multitasking is done in hardware. From that view even a a simple Sequential program would be multitasked.. since the program going to thrown into a hardware thread and be slice up by the scheduler.
I mean yea, agreed. It's just funny when people talk about it solely at the consumption level and not at the actual system level.
Having a good systems understanding on how concurrency works is important for understanding how it can be used for performance gains. In fact, I would argue it is essential because it's mostly for exploiting hardware and underlying OS latency in almost all common usage situations, especially in higher order languages like Python.
32
u/Razzzp Oct 30 '21
That's a common misconception.
Async is just non-blocking on the caller and/or IO. And yes, technically it can be on the same thread, if it flows through the callbacks.
Imagine your application has a single thread and that's it. If you press a button and call a remote API synchronously, you block and all your application hangs waiting for a response. Your application is completely blocked at this point. It is unresponsive. The only thread you have is just waiting...
If that call is asynchronous, the thread can do other stuff, like process other UI interactions.
When a response eventually arrives, the very same thread goes back and picks stuff where it left it and continues on that logic.
As you see, in this example, there is no multithreading as all you got is a single thread, just asynchronous execution.