Event Loop Internals

Preview — 3 of 10 questions

What is the output of the following code?

javascript
async def simple():
    return 42

coro = simple()
try:
    coro.send(None)
except StopIteration as e:
    print(e.value)
ANone
B42
CThe program hangs, since .send(None) never returns.
DTypeError: coroutine.send() takes 0 positional arguments

What is the output of the following code?

javascript
import asyncio

class Later:
    def __await__(self):
        yield
        return "value from Later"

async def main():
    result = await Later()
    print(result)

asyncio.run(main())
ATypeError: object Later can't be used in 'await' expression
BNone
Cvalue from Later
D<generator object Later.__await__ at 0x...>

Which statement correctly describes running asyncio event loops across multiple OS threads?

AOnly the main thread of a Python process is ever allowed to run an event loop; no other thread can run one at all.
BA single asyncio event loop can be freely shared and driven simultaneously by multiple threads without any special handling.
Casyncio automatically creates a new hidden event loop for every coroutine call, regardless of thread.
DEach OS thread that wants to run asyncio coroutines needs its own separate event loop — a single event loop instance is tied to the thread running it and cannot be safely driven concurrently from multiple threads at once.

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.