Flashcards on Asynchronous Programming
Flashcards on Asynchronous Programming
● Overview: Thread and process pools are management techniques used to optimize
the execution of multiple tasks in a program by reusing threads or processes. This
approach reduces overhead, improves performance, and simplifies task
management through an internal queue system.
● Pooling Techniques:
● Resource Optimization:
● Task Execution:
● Performance Improvement:
● Use Cases:
○ Not ideal for scenarios requiring immediate task execution or fine control
over individual threads.
● ThreadPoolExecutor:
● ProcessPoolExecutor:
● Task Scheduling:
● Resource Management:
● Futures:
Coroutines
● Overview: Coroutines are a generalization of subroutines that allow for
cooperative multitasking by enabling functions to yield control back to the event
loop, allowing other tasks to run. They maintain their state between executions,
making them ideal for asynchronous programming and simulating finite state
machines.
● Coroutine Definition:
● Yielding Control:
● State Management:
○ Transitions occur based on input values (e.g., random values from 0 to 1).
○ Each state is defined as a coroutine, managing its own logic and transitions.
Asynchronous Programming
● Overview: Asynchronous programming is a method that allows tasks to be
executed concurrently without blocking the main control flow. It enables efficient
management of I/O-bound operations by using constructs like coroutines and
event loops, allowing for better resource utilization in single-threaded
environments.
● Execution Models:
○ Sequential execution
○ Parallel execution
○ Asynchronous execution
● Event Programming:
● Task Management:
● Concurrency:
● Coroutines:
○ Generalization of subroutines
● Event Loop:
● Futures:
Flashcards
What is the role of an event loop in asynchronous programming?
The event loop manages and processes events in a cyclic manner, ensuring that each
event is handled sequentially when the main thread is free. This structure allows for
efficient event management in asynchronous programming.
What is the main difference between subroutines and coroutines?
Subroutines are called by a main program and cannot run independently, while coroutines
can operate on their own and manage their execution state, allowing for more flexible
control flow and interleaving of tasks without a central coordinator.
What method is used to add a callback to a future object?
The asyncio module handles concurrent tasks by using an event loop to manage
coroutines and futures. Key features include:
Tasks wrap coroutines for execution
The event loop runs one task at a time
While waiting for a future, the loop can execute other tasks
It simplifies writing asynchronous code, making it more readable
The states of the system are S0, S1, S2, S3, and S4, with 0 and 1: the values for which the
automaton can pass from one state to the next state (this operation is called a transition).
So, for example, state S0 can pass to state S1, but only for the value 1, and S0 can pass to
state S2, but only for the value 0.
1. What is the asynchronous programming model? The asynchronous programming
model allows tasks to execute in a non-blocking manner, where tasks can be suspended
and resumed over time without multiple threads.
2. How does the asynchronous model differ from the multithreaded model? In the
multithreaded model, the operating system decides task suspension and resumption, while
in the asynchronous model, this is explicitly controlled by the programmer.
3. What is the key feature of asynchronous programming? Tasks are executed on a single
thread, and while they are not executed simultaneously, they are performed almost at the
same time by suspending and resuming their execution.
5. Name the two main classes in the concurrent.futures module. The main classes are
Executor (abstract class for asynchronous execution) and Future (encapsulates
asynchronous execution of a callable).
7. What is the role of the asyncio module in Python? The asyncio module manages
events, coroutines, tasks, and synchronization primitives for writing concurrent code
using an event loop.
8. Define an event loop in the context of asyncio. An event loop manages and distributes
the execution of tasks by registering tasks and switching control flow from one task to
another.
9. What are coroutines in asyncio? Coroutines are a generalization of subroutines that
can be suspended during execution and resumed later, maintaining their state.
10. What are the main components of asyncio? The main components are the event loop,
coroutines, futures, and tasks.
11. What is a Future object in asyncio? A Future represents a computation or result that
has not yet been completed.
12. What is the role of the asyncio.Task class? It wraps coroutines in tasks, allowing
them to run concurrently in the same event loop.
13. How does the pooling technique benefit server applications? Pooling optimizes
resource usage by reusing threads or processes to manage multiple client requests
efficiently.
14. What is the purpose of the call_soon method in asyncio? It schedules a callback to be
called as soon as possible after the current loop iteration.
15. How does the call_later method in asyncio work? It schedules a callback to be
executed after a specified delay in seconds.
16. Explain the term "yield" in the context of coroutines. "Yield" is used when a
coroutine pauses execution and passes control back to the event loop, allowing other
tasks to run.
17. What does the get_event_loop method do? It retrieves the current event loop for the
execution context.
18. Describe the concept of "task chaining" in asyncio. Task chaining involves
scheduling tasks to call each other in sequence, enabling complex workflows within the
event loop.
19. What is a finite state machine? A finite state machine is a mathematical model
consisting of states and transitions, widely used in engineering and computer science.
20. How does asyncio handle multiple tasks? Asyncio manages multiple tasks by
running them concurrently in the event loop, pausing and resuming tasks as needed.
21. What is the purpose of asyncio.wait? It waits for a collection of tasks or coroutines to
complete.
22. How does pooling reduce thread or process creation overhead? By reusing existing
threads or processes instead of creating new ones, pooling minimizes resource
consumption and improves performance.
23. Why is asynchronous programming preferred for I/O-bound tasks? It allows the
program to handle other tasks while waiting for I/O operations to complete, improving
efficiency.
24. What is the set_result method in asyncio.Future used for? It marks the future as
completed and assigns a result to it.
25. How does the asyncio.Future class relate to the event loop? Future objects interact
with the event loop to manage and track the completion of asynchronous operations.
27. What are the advantages of using coroutines over threads? Coroutines avoid
synchronization issues, are lighter in memory usage, and are better suited for
high-concurrency scenarios.
28. What is the role of synchronization primitives in asyncio? They manage access to
shared resources in concurrent programming to prevent race conditions.
29. How does an event loop ensure fairness among tasks? It cycles through tasks, giving
each a chance to execute, ensuring none are starved.
30. What is the purpose of loop.run_forever in asyncio? It keeps the event loop running
until it is explicitly stopped.
32. How does asyncio manage coroutine state? Asyncio tracks the state of coroutines,
allowing them to be paused and resumed with their context preserved.
33. What is the role of the shutdown method in concurrent.futures? It signals the
executor to release resources and stops accepting new tasks.
35. What are some use cases of asynchronous programming? Use cases include web
servers, real-time data processing, and high-concurrency applications.
36. How does asyncio improve code readability? By using coroutines and futures,
asyncio provides a more structured and less error-prone approach to asynchronous
programming.
38. How does the map method in concurrent.futures work? It applies a function to an
iterable of arguments in asynchronous mode.
39. What is the purpose of the cancel method in asyncio.Future? It cancels a future’s
execution and schedules callbacks to be executed with a cancellation message.
40. What happens when a future object’s result method is called? It retrieves the result of
the future if it is completed, otherwise raises an exception if not ready.
42. Why is reusing threads in a thread pool advantageous? It reduces the overhead of
thread creation and improves the efficiency of repeated tasks.
43. How does asyncio help in handling timeouts? Asyncio allows tasks to be scheduled
with delays and includes timeout management in its methods.
45. What is a task’s lifecycle in asyncio? A task is created, scheduled, executed, and
either completed, canceled, or errored.
46. How does asyncio achieve concurrency on a single thread? By interleaving task
execution using an event loop, suspending tasks when they are idle or waiting.
It shuts down the event loop, cleaning up resources and stopping all tasks.
48. What does "non-blocking" mean in the context of asyncio? It means tasks do not wait
for others to finish before starting; they proceed as soon as resources are available.
49. How does the asyncio module differ from threading? Asyncio operates on a single
thread using cooperative multitasking, whereas threading involves preemptive
multitasking across multiple threads.
50. What is the purpose of the set_exception method in asyncio.Future? It marks the
future as completed with an exception, which can be retrieved when the future is awaited.