Repaso
Español
PythonIntermediatePY-16

Inside a coroutine you write asyncio.sleep(1) without await. What happens?

Options

  1. The sleep runs in the background while the coroutine continues
  2. It creates a coroutine object that never runs, plus a warning (Correct option)
  3. It blocks the whole event loop for one second, then continues
  4. Python raises a SyntaxError because await is required there

Answer

A coroutine object is created and never run; Python warns 'coroutine was never awaited'

Why

Calling an async function does not run its code. It only creates a coroutine object, like a recipe that nobody cooks.

The code runs only when you await it or schedule it with asyncio.create_task(...).

Python prints a 'never awaited' warning to help you find this bug.

Remember

Calling an async function returns a coroutine; nothing runs until it is awaited.

Key terms

Practice this question in Repaso