Bird
Raised Fist0
FastAPIframework~10 mins

Concurrent task execution in FastAPI - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the module needed for running tasks concurrently in FastAPI.

FastAPI
from fastapi import FastAPI
import [1]

app = FastAPI()
Drag options to blanks, or click blank then click option'
Ajson
Brequests
Ctime
Dasyncio
Attempts:
3 left
💡 Hint
Common Mistakes
Importing synchronous modules like requests instead of asyncio.
Forgetting to import any concurrency module.
2fill in blank
medium

Complete the code to define an async endpoint that runs two tasks concurrently using asyncio.

FastAPI
import asyncio
from fastapi import FastAPI

app = FastAPI()

async def task1():
    await asyncio.sleep(1)
    return "Task 1 done"

async def task2():
    await asyncio.sleep(1)
    return "Task 2 done"

@app.get("/run-tasks")
async def run_tasks():
    results = await asyncio.[1](task1(), task2())
    return {"results": results}
Drag options to blanks, or click blank then click option'
Await
Bgather
Crun
Dsleep
Attempts:
3 left
💡 Hint
Common Mistakes
Using asyncio.sleep which just pauses execution.
Using asyncio.wait which returns futures, not results directly.
3fill in blank
hard

Fix the error in the code to correctly run two async tasks concurrently and return their results.

FastAPI
import asyncio
from fastapi import FastAPI

app = FastAPI()

async def task1():
    await asyncio.sleep(1)
    return "Done 1"

async def task2():
    await asyncio.sleep(1)
    return "Done 2"

@app.get("/tasks")
async def tasks():
    results = await asyncio.[1](task1(), task2())
    return {"results": results}
Drag options to blanks, or click blank then click option'
Agather
Brun
Cwait
Dcreate_task
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put await before asyncio.gather.
Using asyncio.run inside an async function.
4fill in blank
hard

Fill both blanks to create a background task in FastAPI that runs concurrently without blocking the response.

FastAPI
from fastapi import FastAPI, BackgroundTasks

app = FastAPI()

def write_log(message: str):
    with open("log.txt", "a") as f:
        f.write(message + "\n")

@app.post("/send-message")
async def send_message(message: str, background_tasks: [1]):
    background_tasks.[2](write_log, message)
    return {"message": "Message received"}
Drag options to blanks, or click blank then click option'
ABackgroundTasks
Bbackground_tasks
Cadd_task
Drun_task
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter type like background_tasks lowercase.
Calling a non-existent method like run_task.
5fill in blank
hard

Fill all three blanks to create a FastAPI endpoint that runs multiple async tasks concurrently and returns their results as a dictionary.

FastAPI
import asyncio
from fastapi import FastAPI

app = FastAPI()

async def fetch_data1():
    await asyncio.sleep(1)
    return "Data 1"

async def fetch_data2():
    await asyncio.sleep(1)
    return "Data 2"

@app.get("/fetch-all")
async def fetch_all():
    results = await asyncio.[1](fetch_data1(), fetch_data2())
    return {"first": results[[2]], "second": results[[3]]}
Drag options to blanks, or click blank then click option'
Agather
B0
C1
Dwait
Attempts:
3 left
💡 Hint
Common Mistakes
Using asyncio.wait which returns futures, not results directly.
Mixing up the indexes for results.

Practice

(1/5)
1. What is the main benefit of using asyncio.create_task() in FastAPI?
easy
A. It stops all tasks from running until manually restarted.
B. It makes the code run slower by adding delays.
C. It converts synchronous code into synchronous code.
D. It allows running multiple tasks at the same time without waiting for each to finish.

Solution

  1. Step 1: Understand asyncio.create_task() purpose

    This function schedules a coroutine to run concurrently as a separate task without blocking the main program.
  2. Step 2: Identify the benefit in FastAPI context

    Using it lets FastAPI handle multiple operations at once, improving responsiveness and efficiency.
  3. Final Answer:

    It allows running multiple tasks at the same time without waiting for each to finish. -> Option D
  4. Quick Check:

    Concurrent task execution = run tasks simultaneously [OK]
Hint: Remember: create_task starts tasks without waiting [OK]
Common Mistakes:
  • Thinking create_task slows code down
  • Confusing async with sync code
  • Believing tasks stop until restarted
2. Which of the following is the correct way to run two async functions concurrently and wait for both to finish in FastAPI?
easy
A. func1(); func2()
B. await asyncio.create_task(func1()); await asyncio.create_task(func2())
C. await asyncio.gather(func1(), func2())
D. asyncio.run(func1(), func2())

Solution

  1. Step 1: Understand how to run and wait for multiple async tasks

    asyncio.gather() runs multiple coroutines concurrently and waits for all to complete.
  2. Step 2: Check each option's correctness

    await asyncio.gather(func1(), func2()) correctly uses await asyncio.gather(func1(), func2()). await asyncio.create_task(func1()); await asyncio.create_task(func2()) starts tasks but awaits them separately, not concurrently. func1(); func2() runs functions synchronously. asyncio.run(func1(), func2()) is invalid syntax.
  3. Final Answer:

    await asyncio.gather(func1(), func2()) -> Option C
  4. Quick Check:

    Use gather to run and wait multiple tasks [OK]
Hint: Use gather to wait all tasks together [OK]
Common Mistakes:
  • Awaiting tasks separately losing concurrency
  • Calling async functions without await
  • Using asyncio.run inside async code
3. Given this FastAPI async endpoint code, what will be the output?
import asyncio
from fastapi import FastAPI
app = FastAPI()

async def task1():
    await asyncio.sleep(1)
    return "Task 1 done"

async def task2():
    await asyncio.sleep(2)
    return "Task 2 done"

@app.get("/run")
async def run_tasks():
    results = await asyncio.gather(task1(), task2())
    return {"results": results}
medium
A. {"results": ["Task 1 done", "Task 2 done"]}
B. {"results": ["Task 2 done", "Task 1 done"]}
C. {"results": ["Task 1 done"]}
D. TimeoutError

Solution

  1. Step 1: Analyze the async tasks and their sleep durations

    task1 sleeps 1 second, task2 sleeps 2 seconds. Both run concurrently with gather.
  2. Step 2: Understand gather result order

    asyncio.gather returns results in the order of the input tasks, regardless of completion time.
  3. Final Answer:

    {"results": ["Task 1 done", "Task 2 done"]} -> Option A
  4. Quick Check:

    Gather preserves input order in results [OK]
Hint: Gather returns results in input order, not finish order [OK]
Common Mistakes:
  • Assuming results order matches finish order
  • Expecting partial results if one task is slower
  • Confusing gather with wait
4. What is wrong with this FastAPI code snippet for running tasks concurrently?
import asyncio
from fastapi import FastAPI
app = FastAPI()

async def task():
    await asyncio.sleep(1)
    return "Done"

@app.get("/test")
async def test():
    task1 = asyncio.create_task(task())
    task2 = asyncio.create_task(task())
    return {"result": task1.result() + task2.result()}
medium
A. Calling task1.result() before task completion causes an error.
B. Using create_task is not allowed in FastAPI endpoints.
C. The tasks should not be async functions.
D. The endpoint should not be async.

Solution

  1. Step 1: Identify how task results are accessed

    Calling task1.result() immediately after creating the task tries to get the result before the task finishes, causing an error.
  2. Step 2: Understand correct way to wait for task completion

    You must await the task or use await asyncio.gather(task1, task2) before accessing results.
  3. Final Answer:

    Calling task1.result() before task completion causes an error. -> Option A
  4. Quick Check:

    Access task result only after await [OK]
Hint: Always await tasks before accessing results [OK]
Common Mistakes:
  • Accessing .result() too early
  • Thinking create_task is forbidden
  • Making endpoint synchronous
5. You want to run three independent async tasks in a FastAPI endpoint and return their results as soon as all finish. Which approach correctly handles errors in any task without stopping others and returns all results or error messages?
hard
A. Use asyncio.create_task() for each task but do not await them before returning.
B. Use asyncio.gather(task1(), task2(), task3(), return_exceptions=True) and process results for exceptions.
C. Use await task1(); await task2(); await task3() sequentially and catch exceptions globally.
D. Run tasks synchronously in a loop and catch exceptions inside each.

Solution

  1. Step 1: Understand error handling in concurrent tasks

    Using asyncio.gather() with return_exceptions=True lets all tasks run concurrently and collects exceptions as results without stopping others.
  2. Step 2: Compare options for correct concurrency and error handling

    Use asyncio.gather(task1(), task2(), task3(), return_exceptions=True) and process results for exceptions. correctly runs all tasks concurrently and handles errors individually. Use await task1(); await task2(); await task3() sequentially and catch exceptions globally. runs tasks sequentially, losing concurrency. Use asyncio.create_task() for each task but do not await them before returning. does not await tasks, so results are unavailable. Run tasks synchronously in a loop and catch exceptions inside each. runs synchronously, blocking concurrency.
  3. Final Answer:

    Use asyncio.gather(task1(), task2(), task3(), return_exceptions=True) and process results for exceptions. -> Option B
  4. Quick Check:

    Gather with return_exceptions handles errors concurrently [OK]
Hint: Use gather with return_exceptions=True for safe concurrency [OK]
Common Mistakes:
  • Awaiting tasks sequentially losing concurrency
  • Not awaiting tasks before returning
  • Ignoring exceptions stopping all tasks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard