0
0
Rest APIprogramming~30 mins

Long-running operations (async responses) in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print() with f-strings to show the messages exactly as described.