TCS fresco play hands on Python Asynchronous Programming
1.What is asynchronous programming in Python?
Asynchronous programming in Python allows for multiple tasks to run concurrently, without blocking the execution of the main program.
2.How does Python support asynchronous programming?
Python supports asynchronous programming through the use of asynchronous frameworks such as asyncio, and the async
and await
keywords.
3.What is the purpose of the async
keyword in Python?
The async
keyword is used to define an asynchronous function, which can be await
-ed to wait for its completion.
4.What is the purpose of the await
keyword in Python?
The await
keyword is used to wait for the completion of an asynchronous function, and to retrieve its result.
5.What is the difference between asynchronous and synchronous programming?
In synchronous programming, the program execution blocks until a task is completed. In asynchronous programming, tasks run concurrently, and the program does not wait for a task to complete.
6.What is the asyncio library in Python?
The asyncio library is a Python library for writing asynchronous code. It provides a framework for writing single-threaded concurrent code using coroutines, multiplexing I/O access over sockets and other resources, running network clients and servers, and other related primitives.
7.Why use asynchronous programming in Python?
Asynchronous programming can improve the performance and responsiveness of programs by allowing tasks to run concurrently, without blocking the main program execution. It is especially useful in cases where there is I/O-bound or high-level network code.
8.What is a coroutine in Python?
A coroutine is a special type of function that can be suspended and resumed. Coroutines can be used to write asynchronous code in a more intuitive and straightforward way, compared to traditional thread-based or callback-based approaches.
9.What is the async for
statement in Python?
The async for
statement is used to iterate over an asynchronous iterable in a coroutine. It allows for asynchronous iteration, which can be useful in cases where the iterable is I/O-bound or slow.
10.Can you use the await
keyword outside of an async
function?
No, the await
keyword can only be used inside an async
function. If used outside of an async
function, it will raise a SyntaxError.
11.What is a Future in Python asyncio?
A Future is a special object in asyncio that represents the result of a coroutine. It can be used to track the completion of a coroutine, retrieve its result, and cancel it if necessary.
12.What is a Task in Python asyncio?
A Task is a subclass of Future that wraps a coroutine. It is used to schedule a coroutine to run in the event loop and keep track of its execution.
13.How do you run a coroutine in asyncio?
To run a coroutine in asyncio, you need to schedule it with the event loop using the create_task
method or the ensure_future
function. The event loop will then run the coroutine asynchronously, until it is either complete or blocked.
14.What is the difference between asyncio.run and asyncio.run_until_complete?
asyncio.run
is a convenience function that sets up an event loop, runs a coroutine, and automatically closes the loop when the coroutine is complete. asyncio.run_until_complete
is a similar function, but it only runs the coroutine until it is complete, without closing the event loop.
15.What is a context manager in Python?
A context manager is an object that defines the methods __enter__
and __exit__
. These methods are called when entering and exiting a with
statement, respectively. Context managers are used to manage resources, such as opening and closing files, acquiring and releasing locks, and so on.
16.How do you use a context manager in an asynchronous function?
To use a context manager in an asynchronous function, you can use the async with
statement. This allows you to use context managers in an asynchronous way, by automatically suspending and resuming the coroutine as necessary.