Handling Long-Running Operations with Async Responses in REST API
📖 Scenario: You are building a REST API that processes large files. Since processing takes time, the API should respond immediately with a job ID and let the client check the job status later.
🎯 Goal: Create a simple REST API that starts a long-running job asynchronously, returns a job ID immediately, and allows clients to check the job status with that ID.
📋 What You'll Learn
Create a dictionary called jobs to store job statuses
Create a variable called job_counter starting at 1
Write a function start_job() that adds a new job with status 'running' and returns the job ID
Write a function check_status(job_id) that returns the status of the job
Print the job ID after starting a job
Print the status of a job when checked
💡 Why This Matters
🌍 Real World
Many web services handle tasks that take time, like file uploads or data processing. They respond immediately with a job ID and let users check progress later.
💼 Career
Understanding async responses and job tracking is important for backend developers, API designers, and anyone building scalable web services.
Progress0 / 4 steps
1
Create the initial data structure
Create a dictionary called jobs to store job statuses and a variable called job_counter set to 1.
Rest API
Hint
Use {} to create an empty dictionary and assign 1 to job_counter.
2
Add a function to start a job
Write a function called start_job() that uses the global job_counter, adds a new entry to jobs with status 'running', increments job_counter, and returns the job ID.
Rest API
Hint
Remember to use global job_counter inside the function to modify it.
3
Add a function to check job status
Write a function called check_status(job_id) that returns the status of the job from the jobs dictionary using the given job_id. If the job ID is not found, return 'not found'.
Rest API
Hint
Use the dictionary get() method to safely get the status or return 'not found'.
4
Start a job and print its status
Call start_job() and save the result in job_id. Then print the job ID with the message "Started job with ID: {job_id}". Next, call check_status(job_id) and print the status with the message "Status of job {job_id}: {status}".
Rest API
Hint
Use print() with f-strings to show the messages exactly as described.
Practice
(1/5)
1. What is the main purpose of using long-running operations with async responses in REST APIs?
easy
A. To make the server process all requests instantly.
B. To let clients start a task and check its progress later without waiting.
C. To block the client until the task finishes.
D. To avoid using URLs for task status.
Solution
Step 1: Understand client-server interaction in async operations
Clients start a task and receive a URL to check progress later, avoiding waiting.
Step 2: Identify the benefit of async responses
This approach keeps apps responsive and user-friendly by not blocking clients.
Final Answer:
To let clients start a task and check its progress later without waiting. -> Option B
Quick Check:
Async responses = clients check progress later [OK]
Hint: Async means start task, check later URL [OK]
Common Mistakes:
Thinking server processes all requests instantly
Assuming client waits for task completion
Ignoring the use of status URLs
2. Which HTTP status code is typically returned immediately by a REST API to indicate a long-running operation has started asynchronously?
easy
A. 200 OK
B. 404 Not Found
C. 202 Accepted
D. 500 Internal Server Error
Solution
Step 1: Recall HTTP status codes for async start
202 Accepted means the request is accepted but processing is not complete yet.
Step 2: Compare with other codes
200 OK means completed, 404 means missing resource, 500 means server error.
Final Answer:
202 Accepted -> Option C
Quick Check:
Async start = 202 Accepted [OK]
Hint: 202 means accepted but not done yet [OK]
Common Mistakes:
Using 200 OK for async start
Confusing 404 with async status
Assuming 500 means task started
3. Given this REST API response after starting a long-running task: