0
0
Rest APIprogramming~20 mins

Async batch processing in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Batch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this async batch processing code?

Consider a REST API that processes a batch of tasks asynchronously. The server returns a job ID immediately, and the client polls the job status until completion.

What will be printed by the client code below?

Rest API
import asyncio

async def check_status(job_id):
    # Simulate job completion after 3 checks
    for i in range(3):
        print(f"Checking status for job {job_id}, attempt {i+1}")
        await asyncio.sleep(0.1)
    return "completed"

async def main():
    job_id = "job123"
    status = await check_status(job_id)
    print(f"Job {job_id} status: {status}")

asyncio.run(main())
A
Checking status for job job123, attempt 1
Checking status for job job123, attempt 2
Job job123 status: completed
Checking status for job job123, attempt 3
B
Job job123 status: completed
Checking status for job job123, attempt 1
Checking status for job job123, attempt 2
Checking status for job job123, attempt 3
C
Checking status for job job123, attempt 1
Job job123 status: completed
Checking status for job job123, attempt 2
Checking status for job job123, attempt 3
D
Checking status for job job123, attempt 1
Checking status for job job123, attempt 2
Checking status for job job123, attempt 3
Job job123 status: completed
Attempts:
2 left
💡 Hint

Remember that await pauses the function until the awaited coroutine finishes.

🧠 Conceptual
intermediate
1:00remaining
Which HTTP method is best for submitting an async batch job to a REST API?

You want to send a batch of tasks to a REST API that will process them asynchronously. Which HTTP method should you use to submit the batch job?

APOST
BGET
CPUT
DDELETE
Attempts:
2 left
💡 Hint

Think about which method is used to create new resources or submit data.

🔧 Debug
advanced
2:30remaining
Why does this async batch polling code never stop?

Look at this client code that polls a batch job status from a REST API. It never stops polling. Why?

Rest API
import asyncio
import random

async def get_status(job_id):
    # Simulate random job status
    return random.choice(["pending", "pending", "completed"])

async def poll_job(job_id):
    status = "pending"
    while status != "completed":
        status = await get_status(job_id)
        print(f"Job {job_id} status: {status}")
        await asyncio.sleep(0.1)

asyncio.run(poll_job("job456"))
AThe code is missing a break statement inside the loop.
BThe random choice includes "pending" twice, so it might never pick "completed".
CThe await asyncio.sleep(0.1) is too short to allow status update.
DThe function get_status is not awaited properly.
Attempts:
2 left
💡 Hint

Look at the probabilities of the status values returned.

📝 Syntax
advanced
1:30remaining
Which option correctly defines an async function to submit a batch job and return job ID?

Choose the correct Python async function syntax that submits a batch job and returns a job ID string.

A
async def submit_batch():
    job_id = "job789"
    yield job_id
B
def async submit_batch():
    job_id = "job789"
    return job_id
C
async def submit_batch():
    job_id = "job789"
    return job_id
D
async submit_batch() -> str:
    job_id = "job789"
    return job_id
Attempts:
2 left
💡 Hint

Remember the correct order of keywords for async function definitions.

🚀 Application
expert
2:00remaining
How many items will be in the batch result after processing this async batch?

This code submits a batch of 5 tasks asynchronously and collects results. How many items will be in the results list after completion?

Rest API
import asyncio

async def process_task(task_id):
    await asyncio.sleep(0.1)
    return f"result_{task_id}"

async def process_batch(tasks):
    results = []
    for task in tasks:
        result = await process_task(task)
        results.append(result)
    return results

batch_tasks = [1, 2, 3, 4, 5]
results = asyncio.run(process_batch(batch_tasks))
print(len(results))
A5
B0
C1
DRaises an exception
Attempts:
2 left
💡 Hint

Each task is awaited and its result appended to the list.