sanic/examples/limit_concurrency.py
Mohammad Almoghrabi f2cc83c1ba
fix examples for freeze_support() issue on windows (#2741)
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
2023-07-05 13:45:08 +03:00

44 lines
835 B
Python

import asyncio
import httpx
from sanic import Sanic
from sanic.response import json
app = Sanic("Example")
sem = None
@app.before_server_start
def init(sanic, _):
global sem
concurrency_per_worker = 4
sem = asyncio.Semaphore(concurrency_per_worker)
async def bounded_fetch(session, url):
"""
Use session object to perform 'get' request on url
"""
async with sem:
response = await session.get(url)
return response.json()
@app.route("/")
async def test(request):
"""
Download and serve example JSON
"""
url = "https://api.github.com/repos/sanic-org/sanic"
async with httpx.AsyncClient() as session:
response = await bounded_fetch(session, url)
return json(response)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, workers=2)