Last active
April 1, 2018 21:39
-
-
Save pyk/a20eb40fc5cf6df76bda083836b7a6b4 to your computer and use it in GitHub Desktop.
sanic-instrumentation-guide
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app.py | |
import asyncio | |
import random | |
from sanic import Sanic | |
from sanic import response | |
app = Sanic() | |
@app.get("/") | |
async def index(request): | |
# Simulate latency: 0ms to 1s | |
latency = random.random() # in seconds | |
await asyncio.sleep(latency) | |
return response.json({"message": "Hello there!"}) | |
@app.get("/products") | |
async def products(request): | |
products = [ | |
{"title": "product_a", "price": 10.0}, | |
{"title": "product_b", "price": 5.0}, | |
] | |
# Simulate latency: 0ms to 1s | |
latency = random.random() # in seconds | |
await asyncio.sleep(latency) | |
return response.json(products) | |
@app.post("/order") | |
async def order(request): | |
# Simulate latency: 0ms to 1s | |
latency = (1 - random.random()) # in seconds | |
await asyncio.sleep(latency) | |
return response.json({"message": "OK"}) | |
if __name__ == "__main__": | |
app.run(host="0.0.0.0", port=8080) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment