Skip to content
tezvyn:

What is the difference between async def and regular functions?

Source: docs.python.orgEasyHow cards are made

What is the difference between async def and regular functions?

This tests async def call semantics. A strong answer contrasts regular functions, which execute immediately, with async def, which returns a coroutine object that does nothing until awaited or wrapped in create_task.

What's really being asked

Whether the candidate understands the distinction between a coroutine function and a coroutine object, and whether they know that calling an async def function does not execute its body. The interviewer is looking for awareness of the event loop as the driver of execution and the specific mechanisms that move a coroutine from created to running.

The full answer

First, the candidate should state that a regular function begins executing its body immediately upon call and returns a value when complete. Second, they should explain that an async def function, when called, returns a coroutine object; the function body does not run yet. Third, they should list the three primary ways this object is executed: awaiting it directly inside another coroutine, wrapping it with asyncio.create_task to schedule it concurrently on the event loop, or using asyncio.run for the top-level entry point. Fourth, they should mention that a coroutine object is an awaitable, which is a prerequisite for these mechanisms.

The mistakes people make

The biggest red flag is claiming that calling an async def function runs the code immediately like a regular function. Another mistake is saying that async def makes a function run in parallel automatically; it merely provides a construct for cooperative multitasking. Some candidates forget that a bare call without await or create_task produces an unawaited coroutine object, which triggers a RuntimeWarning and never executes. Confusing coroutine functions with Tasks or Futures is also a signal of shallow understanding.

What usually comes next

The interviewer may ask how asyncio.create_task differs from a bare await, which opens a discussion about concurrency versus sequential execution. They might ask what a Task is, leading to the relationship between Tasks, the event loop, and the underlying Future. Another common follow-up is how to run async code from a synchronous context, which touches on asyncio.run and thread safety. Finally, they may ask about the async for and async with statements to probe comprehension of the broader async syntax.

A concrete example

Consider an async def fetch_data(): return 42. If you write result = fetch_data(), result is a coroutine object, not the integer 42. The function body has not executed. To get the value, you must write value = await result inside another async function, or schedule it with task = asyncio.create_task(fetch_data()) and later await task. If you simply call fetch_data() and discard the return value, CPython warns RuntimeWarning: coroutine fetch_data was never awaited because the event loop never received the object.

Interview question

What is the immediate result of calling an async def function without using await?

  • a.It executes up to the first await expression and then pauses
  • b.A Task is automatically scheduled to run on the event loop
  • c.The function body executes and returns the final value
  • d.A coroutine object is returned but the body has not yet executedCorrect
Why?

Calling an async def function returns a coroutine object immediately without running any of the function body; execution only begins when the object is awaited or wrapped in create_task. The distractor that it runs up to the first await is wrong because the body does not start executing at all upon the bare call.

Just read this? Test yourself on what you have been reading.

Read the original → docs.python.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on python — each one lists the topics its interview covers.

See open roles