0
0
Rest APIprogramming~30 mins

Async batch processing in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
Async Batch Processing with REST API
📖 Scenario: You are building a REST API that processes a batch of tasks asynchronously. This is useful when you have many tasks to run but don't want to block the client while waiting for all tasks to finish.Imagine a system that receives a list of job IDs and processes each job in the background, returning a batch ID immediately. Later, the client can check the batch status or results.
🎯 Goal: Create a simple REST API with endpoints to submit a batch of jobs for asynchronous processing, track their progress, and retrieve results.
📋 What You'll Learn
Create an endpoint /submit_batch that accepts a JSON list of job IDs and returns a batch ID immediately.
Store the batch jobs and their statuses in a dictionary.
Process each job asynchronously with a simulated delay.
Create an endpoint /batch_status that returns the status of all jobs in a batch.
Create an endpoint /batch_results that returns the results of all completed jobs in a batch.
💡 Why This Matters
🌍 Real World
Async batch processing is common in web services that handle many tasks without blocking clients, such as image processing, data import, or sending emails.
💼 Career
Understanding async batch processing is valuable for backend developers, API designers, and anyone working with scalable web applications or microservices.
Progress0 / 4 steps
1
Setup initial data structures
Create a dictionary called batches to store batch information. Each batch will have a unique ID as key and a dictionary with job statuses as value. Also, create a variable batch_counter initialized to 0 to generate batch IDs.
Rest API
Need a hint?

Use a dictionary for batches and an integer batch_counter starting at 0.

2
Create batch submission endpoint
Write a function submit_batch(job_ids) that increments batch_counter, creates a new entry in batches with the new batch ID, and initializes each job ID with status 'pending'. Return the new batch ID.
Rest API
Need a hint?

Use a dictionary comprehension to set each job's status to 'pending'. Remember to declare batch_counter as global to modify it.

3
Process jobs asynchronously
Write an async function process_batch(batch_id) that loops over each job in batches[batch_id], simulates processing with await asyncio.sleep(1), and updates the job status to 'done'.
Rest API
Need a hint?

Use async def and await asyncio.sleep(1) to simulate asynchronous processing.

4
Display batch status and results
Write two functions: batch_status(batch_id) that returns the dictionary of job statuses for the batch, and batch_results(batch_id) that returns a list of job IDs with status 'done'. Then, print the status and results for batch ID 1 after processing it asynchronously.
Rest API
Need a hint?

Use dictionary get method to safely access batches. Use list comprehension to filter jobs with status 'done'. Use asyncio.run(main()) to run the async main function.