Task (async)¶
Category: Units of execution · Status: stub · Lessons: chapter 06, Async (planned)
One line: A unit of async work handed to a runtime — a future being driven to completion — far cheaper than a thread because it holds no stack of its own while it waits.
Also called: task, spawned future.
How it connects¶
flowchart LR
n_future_and_promise["Future and promise"]
n_async_task["Task (async)"]
n_thread["Thread"]
n_async_task ---|vs| n_thread
n_async_task -->|uses| n_future_and_promise
classDef center stroke-width:3px
class n_async_task center
classDef outside stroke-dasharray: 4 3
class n_future_and_promise,n_thread outside
- Is built on: Future and promise
- Often confused with: Thread
- See also: Async runtime (executor and reactor), Thread-local storage
In each language¶
| Rust | tokio::spawn ↗ hands a future to the Tokio runtime and returns a JoinHandle |
| Python | asyncio.create_task ↗; keep a reference to the task, since the event loop holds only a weak one |
| C# | Task ↗ represents an asynchronous operation |
| JavaScript | Calling an async function ↗ runs it synchronously up to its first await and returns a Promise |
| Kotlin | async ↗ returns a Deferred with a result; launch ↗ returns a Job |
| Swift | Task ↗, a unit of asynchronous work |
| Erlang and Elixir | Task.async ↗ starts a process linked to the caller, and Task.await receives its reply |
| Haskell | async ↗ runs an IO action in a separate thread and returns an Async handle |
Where to read more¶
- In the books: Concurrency in .NET, Riccardo Terrell — ch. 7, 'Task-based functional parallelism'
- In the books: Python Asyncio Jump-Start, Jason Brownlee — ch. 2, 'Coroutines and Tasks'
- In the books: Pro Asynchronous Programming with .NET, Richard Blewett, Andrew Clymer — ch. 3, 'Tasks'
- In the books: Async Rust, Maxwell Flitton, Caroline Morton — ch. 2, 'Basic Async Rust' → 'Understanding Tasks'
- In the books: Concurrency with Modern C++, Rainer Grimm — ch. 3, 'Multithreading' → 'Tasks'
- In the books: C++ Reactive Programming, Praseed Pai, Peter Abraham — ch. 4, 'Asynchronous and Lock-Free Programming in C++' → 'Task-based parallelism in C++'