Skip to content

Instantly share code, notes, and snippets.

@pyk
Last active April 1, 2018 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pyk/a20eb40fc5cf6df76bda083836b7a6b4 to your computer and use it in GitHub Desktop.
Save pyk/a20eb40fc5cf6df76bda083836b7a6b4 to your computer and use it in GitHub Desktop.
sanic-instrumentation-guide
# 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