How to Mock a 202 Accepted Long-Running Operation in Python
Build a mock HTTP server that returns a 202 Accepted response immediately and simulates a long-running operation in the background with threading.
Python code
30 linesimport time
import threading
from http.server import HTTPServer, BaseHTTPRequestHandler
class MockHandler(BaseHTTPRequestHandler):
def do_POST(self):
if self.path == "/long-running":
self.send_response(202)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(b'{"status": "accepted", "job_id": "12345"}')
# Simulate long-running work in background
threading.Thread(target=self._process, daemon=True).start()
def _process(self):
time.sleep(3) # Simulate 3-second operation
print("Long-running operation completed")
def log_message(self, format, *args):
pass # Silence default logging
def run():
server = HTTPServer(("127.0.0.1", 8000), MockHandler)
print("Mock server running on http://127.0.0.1:8000")
print("POST /long-running returns 202 Accepted immediately")
server.serve_forever()
if __name__ == "__main__":
run()
Output
Mock server running on http://127.0.0.1:8000
POST /long-running returns 202 Accepted immediately
Long-running operation completed
How it works
The do_POST method checks the request path and returns a 202 Accepted status with a JSON body immediately. A background thread is started using threading.Thread with daemon=True so it does not block the server. The time.sleep(3) call simulates real work, and the completion message appears on the server console, not in the HTTP response. Silencing log_message keeps the mock server's output clean for debugging.
Common mistakes
- Blocking the request handler with `time.sleep` before returning the response, which defeats the purpose of 202
- Forgetting to set `daemon=True` on the background thread, preventing the server from shutting down cleanly
- Trying to include the long-running result in the same HTTP response instead of returning it in a separate status endpoint
Variations
- Use `concurrent.futures.ThreadPoolExecutor` to manage multiple background jobs instead of raw threads
- Add a separate `GET /status/{job_id}` endpoint so clients can poll for completion
Real-world use cases
- Mocking an async job queue during integration testing of API clients that expect 202 patterns.
- Simulating a report generation service where the API acknowledges the request and processes it in the background.
- Creating a fake webhook receiver that accepts events instantly for load testing consumer pipelines.
Sponsored
More from API design & gRPC
- Build a Bulk Array POST Mock Server in Python medium
- Build a Mock REST API with PUT and GET in Python medium
- Convert Protobuf to JSON and Dict in Python easy
- Create a Data Helper in Python for gRPC-style APIs easy
- Format data in Python using dataclasses like gRPC messages easy
- Generate an OpenAPI Spec from Mock Routes in Python easy
Keep learning
Related tutorials and quizzes for this topic.