Event Loop Internals — Series 3

Preview — 3 of 10 questions

What is the output of the following code?

javascript
import asyncio

async def main():
    sem = asyncio.BoundedSemaphore(2)
    await sem.acquire()
    await sem.acquire()
    sem.release()
    sem.release()
    try:
        sem.release()
    except ValueError as e:
        print("ValueError:", e)

asyncio.run(main())
AValueError: BoundedSemaphore released too many times
BNo error — the semaphore's internal count simply keeps increasing
CRuntimeError: semaphore already at maximum value
DNothing happens; the third release() call is silently ignored

What is the output of the following code?

javascript
import asyncio
import time

def blocking_work():
    time.sleep(0.05)
    return "done"

async def main():
    loop = asyncio.get_running_loop()
    result = await loop.run_in_executor(None, blocking_work)
    print(result)

asyncio.run(main())
ARuntimeError: blocking_work() cannot be called from an executor
Bdone
CTypeError: run_in_executor() missing 1 required positional argument
DA coroutine object prints instead of the string, since run_in_executor() doesn't actually wait for completion

What is the output of the following code?

javascript
import asyncio

async def worker():
    return 1

async def main():
    coro = worker()
    task = asyncio.create_task(coro)
    print(asyncio.iscoroutine(coro))
    print(asyncio.iscoroutine(task))
    await task

asyncio.run(main())
AFalse, True
BFalse, False
CTrue, False
DTrue, True

Sign up free to play

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