在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
✨ The little ASGI framework that shines. ✨ Documentation: https://www.starlette.io/ StarletteStarlette is a lightweight ASGI framework/toolkit,which is ideal for building high performance asyncio services. It is production-ready, and gives you the following:
RequirementsPython 3.6+ Installation$ pip3 install starlette You'll also want to install an ASGI server, such as uvicorn, daphne, or hypercorn. $ pip3 install uvicorn Examplefrom starlette.applications import Starlettefrom starlette.responses import JSONResponseimport uvicornapp = Starlette(debug=True)@app.route('/')async def homepage(request): return JSONResponse({'hello': 'world'})if __name__ == '__main__': uvicorn.run(app, host='0.0.0.0', port=8000) For a more complete example, see encode/starlette-example. DependenciesStarlette does not have any hard dependencies, but the following are optional:
You can install all of these with Framework or ToolkitStarlette is designed to be used either as a complete framework, or asan ASGI toolkit. You can use any of its components independently. from starlette.responses import PlainTextResponseclass App: def __init__(self, scope): assert scope['type'] == 'http' self.scope = scope async def __call__(self, receive, send): response = PlainTextResponse('Hello, world!') await response(receive, send) Run the $ uvicorn example:AppINFO: Started server process [11509]INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) Run uvicorn with ModularityThe modularity that Starlette is designed on promotes building re-usablecomponents that can be shared between any ASGI framework. This should enablean ecosystem of shared middleware and mountable applications. The clean API separation also means it's easier to understand each componentin isolation. PerformanceIndependent TechEmpower benchmarks show Starlette applications running under Uvicornas one of the fastest Python frameworks available. (*) For high throughput loads you should:
Eg. gunicorn -w 4 -k uvicorn.workers.UvicornWorker --log-level warning example:app Several of the ASGI servers also have pure Python implementations available,so you can also run under Either programatically: uvicorn.run(..., http='h11', loop='asyncio') Or using Gunicorn: gunicorn -k uvicorn.workers.UvicornH11Worker ... — ⭐️ — Starlette is BSD licensed code. Designed & built in Brighton, England. |
请发表评论