All quizzesHard
Event Loop Internals — Series 2
Preview — 3 of 10 questions
What is the output of the following code?
javascript
import asyncio
async def worker():
await asyncio.sleep(0.01)
async def main():
task = asyncio.create_task(worker())
coro = task.get_coro()
print(coro is not None)
print(asyncio.iscoroutine(coro))
await task
asyncio.run(main())ATrue, False — the coroutine returned by get_coro() is a distinct copy, not recognized as a real coroutine object.
BTrue, True
CAttributeError: 'Task' object has no attribute 'get_coro'
DFalse, False — a Task doesn't retain a reference to its original coroutine once it starts running.
What is the output of the following code?
javascript
import asyncio
async def worker():
try:
await asyncio.sleep(10)
except ValueError as e:
print("caught:", e)
return "recovered"
async def main():
coro = worker()
coro.send(None)
try:
result = coro.throw(ValueError("injected"))
except StopIteration as e:
print("result:", e.value)
asyncio.run(main())Acaught: injected, then RuntimeError: coroutine raised StopIteration
BNothing is caught — the injected ValueError propagates straight out of .throw() uncaught.
Ccaught: injected, then result: None — the coroutine's return value isn't captured by StopIteration.value when triggered via .throw().
Dcaught: injected, then result: recovered
What is the output of the following code?
javascript
import asyncio
async def main():
loop = asyncio.get_running_loop()
log = []
loop.call_soon(lambda: log.append("first"))
loop.call_soon(lambda: log.append("second"))
loop.call_soon(lambda: log.append("third"))
await asyncio.sleep(0.01)
print(log)
asyncio.run(main())A['first', 'second', 'third']
B['third', 'second', 'first'] — call_soon schedules callbacks onto a stack, so the most recently scheduled one runs first.
CUnpredictable order, since call_soon doesn't guarantee any particular scheduling order.
DTypeError: call_soon() missing required argument
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.