The little ASGI framework that shines. 🌟
Go to file
Thomas Grainger 254d0d97e4
ensure TestClient requests run in the same EventLoop as lifespan (#1213)
* ensure TestClient requests run in the same EventLoop as lifespan

* for lifespan task verification, use native task identity rather than anyio.abc.TaskInfo equality

https://github.com/agronholm/anyio/issues/324

* remove redundant pragma: no cover

* it's now a loop_id not a threading.ident

* replace Protocol with plain Callable TypeAlias

* use lifespan_context to actually open a task group

trio should complain if used incorrectly here.

* assign self.portal once, schedule reset immediately after assignment

* inline apps into their tests

* make task/loop trackers nonlocals
2021-07-03 17:39:25 +01:00
.github 🔧 Add funding option (#1219) 2021-06-28 12:15:08 +02:00
docs TestClient accepts backend and backend_options as arguments to constructor (#1211) 2021-06-28 21:36:13 +01:00
scripts Use coverage directly instead of pytest-cov (#1204) 2021-06-19 12:42:56 +01:00
starlette ensure TestClient requests run in the same EventLoop as lifespan (#1213) 2021-07-03 17:39:25 +01:00
tests ensure TestClient requests run in the same EventLoop as lifespan (#1213) 2021-07-03 17:39:25 +01:00
.gitignore Update CI scripts to match httpcore (#1043) 2020-09-06 12:08:07 +02:00
LICENSE.md Update link to https (#356) 2019-01-25 23:12:32 +00:00
MANIFEST.in MANIFEST.ini (#895) 2020-04-14 10:23:46 +01:00
README.md anyio integration (#1157) 2021-06-18 15:48:43 +01:00
mkdocs.yml mkdocs: Set site_url (#1215) 2021-06-25 08:46:38 +01:00
requirements.txt Test on Python 3.10 (#1201) 2021-06-19 18:02:53 +01:00
setup.cfg Test on Python 3.10 (#1201) 2021-06-19 18:02:53 +01:00
setup.py TestClient accepts backend and backend_options as arguments to constructor (#1211) 2021-06-28 21:36:13 +01:00

README.md

starlette

The little ASGI framework that shines.

Build Status Package version


Documentation: https://www.starlette.io/


Starlette

Starlette is a lightweight ASGI framework/toolkit, which is ideal for building high performance async services.

It is production-ready, and gives you the following:

  • Seriously impressive performance.
  • WebSocket support.
  • GraphQL support.
  • In-process background tasks.
  • Startup and shutdown events.
  • Test client built on requests.
  • CORS, GZip, Static Files, Streaming responses.
  • Session and Cookie support.
  • 100% test coverage.
  • 100% type annotated codebase.
  • Few hard dependencies.
  • Compatible with asyncio and trio backends.

Requirements

Python 3.6+

Installation

$ pip3 install starlette

You'll also want to install an ASGI server, such as uvicorn, daphne, or hypercorn.

$ pip3 install uvicorn

Example

example.py:

from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route


async def homepage(request):
    return JSONResponse({'hello': 'world'})

routes = [
    Route("/", endpoint=homepage)
]

app = Starlette(debug=True, routes=routes)

Then run the application using Uvicorn:

$ uvicorn example:app

For a more complete example, see encode/starlette-example.

Dependencies

Starlette only requires anyio, and the following are optional:

  • requests - Required if you want to use the TestClient.
  • jinja2 - Required if you want to use Jinja2Templates.
  • python-multipart - Required if you want to support form parsing, with request.form().
  • itsdangerous - Required for SessionMiddleware support.
  • pyyaml - Required for SchemaGenerator support.
  • graphene - Required for GraphQLApp support.

You can install all of these with pip3 install starlette[full].

Framework or Toolkit

Starlette is designed to be used either as a complete framework, or as an ASGI toolkit. You can use any of its components independently.

from starlette.responses import PlainTextResponse


async def app(scope, receive, send):
    assert scope['type'] == 'http'
    response = PlainTextResponse('Hello, world!')
    await response(scope, receive, send)

Run the app application in example.py:

$ uvicorn example:app
INFO: Started server process [11509]
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

Run uvicorn with --reload to enable auto-reloading on code changes.

Modularity

The modularity that Starlette is designed on promotes building re-usable components that can be shared between any ASGI framework. This should enable an ecosystem of shared middleware and mountable applications.

The clean API separation also means it's easier to understand each component in isolation.

Performance

Independent TechEmpower benchmarks show Starlette applications running under Uvicorn as one of the fastest Python frameworks available. (*)

For high throughput loads you should:

  • Run using gunicorn using the uvicorn worker class.
  • Use one or two workers per-CPU core. (You might need to experiment with this.)
  • Disable access logging.

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 PyPy if your application code has parts that are CPU constrained.

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.