The lightning-fast ASGI server. ?

Overview

uvicorn

The lightning-fast ASGI server.


Build Status Package version

Documentation: https://www.uvicorn.org

Community: https://discuss.encode.io/c/uvicorn

Requirements: Python 3.6+ (For Python 3.5 support, install version 0.8.6.)

Uvicorn is a lightning-fast ASGI server implementation, using uvloop and httptools.

Until recently Python has lacked a minimal low-level server/application interface for asyncio frameworks. The ASGI specification fills this gap, and means we're now able to start building a common set of tooling usable across all asyncio frameworks.

Uvicorn currently supports HTTP/1.1 and WebSockets. Support for HTTP/2 is planned.

Quickstart

Install using pip:

$ pip install uvicorn

This will install uvicorn with minimal (pure Python) dependencies.

$ pip install uvicorn[standard]

This will install uvicorn with "Cython-based" dependencies (where possible) and other "optional extras".

In this context, "Cython-based" means the following:

  • the event loop uvloop will be installed and used if possible.
  • the http protocol will be handled by httptools if possible.

Moreover, "optional extras" means that:

  • the websocket protocol will be handled by websockets (should you want to use wsproto you'd need to install it manually) if possible.
  • the --reloader flag in development mode will use watchgod.
  • windows users will have colorama installed for the colored logs.
  • python-dotenv will be installed should you want to use the --env-file option.
  • PyYAML will be installed to allow you to provide a .yaml file to --log-config, if desired.

Create an application, in example.py:

async def app(scope, receive, send):
    assert scope['type'] == 'http'

    await send({
        'type': 'http.response.start',
        'status': 200,
        'headers': [
            [b'content-type', b'text/plain'],
        ],
    })
    await send({
        'type': 'http.response.body',
        'body': b'Hello, world!',
    })

Run the server:

$ uvicorn example:app

Uvicorn is BSD licensed code.
Designed & built in Brighton, England.

?

Comments
  • Sporatic errors with nginx connection pipelining

    Sporatic errors with nginx connection pipelining

    There seems to be some weird issue with pipelining and or connection reuse and nginx. I have nginx sitting in front of my uvicorn/starlette app, and on patch/post requests I occasionally get 502's from nginx (about 30-40% of the time). The nginx logs say:

    upstream prematurely closed connection while reading response header from upstream
    

    all over the place

    and my app logs say:

    WARNING: Invalid HTTP request received.
    WARNING: Invalid HTTP request received.
    

    with not much more information.

    I forced connection reuse to not work by adding the default header Connection: close, which forces nginx to kill the connection. Performance drops significantly, but at least I don't get 502's.

    uvicorn.run(app, host='0.0.0.0', port=8445, headers=[('Server', 'custom'), ('Connection', 'close')], proxy_headers=True)
    

    potentially relevant: the 502 doesn't seem to ever happen on a GET

    bug http/httptools uds 
    opened by nhumrich 67
  • Using UvicornWorkers in Gunicorn cause OOM on K8s

    Using UvicornWorkers in Gunicorn cause OOM on K8s

    Checklist

    • [X] The bug is reproducible against the latest release and/or master.
    • [X] There are no similar issues or pull requests to fix it yet.

    Describe the bug

    I'm developing a FastApi application deployed on a Kubernetes cluster using gunicorn as process manager.

    I'm also using UvicornWorkers for sure, because of the async nature of fastapi.

    After the application deployment I can see the memory growing up at rest, until OOM.

    This happen just when I use UvicornWorker.

    Tests made by me:

    • Comment all my code to ensure is not a my application mem leak (leak present);
    • Start the application using uvicorn instead of gunicorn (no leak present);
    • Start application using gunicorn sync workers (no leak present);
    • Start application using gunicorn + UvicornWorker (leak present);
    • Start application using gunicorn + UvicornWorker + max_requests (leak present);

    Plus, this happens just on the Kubernetes cluster, when I run my application locally (MacBook pro 16) (is the same docker image used on k8s) the leak is not present.

    Anyone else had a similar problem?

    bug help wanted 
    opened by KiraPC 41
  • Run uvicorn with multiple workers on Windows - WinError 87

    Run uvicorn with multiple workers on Windows - WinError 87

    Describe the bug Uvicorn doesn't seem to be able to start with multiple workers.

    Code snippet Starlette Hello World

    import uvicorn as uvicorn
    from starlette.applications import Starlette
    from starlette.responses import JSONResponse
    
    app = Starlette(debug=True)
    
    
    @app.route('/')
    async def homepage(request):
        return JSONResponse({'hello': 'world'})
    
    
    if __name__ == '__main__':
        uvicorn.run(app, host='127.0.0.1', port=8000, workers=2)
    
    

    Expected behavior

    Uvicorn starts and is ready to serve with multiple workers.

    Environment

    Windows 10 Uvicorn: 0.6.1 Python: 3.7.2 Starlette: 0.11.4

    Additional context

    (venv) C:\Users\marodev\source\repos\StarletteHelloWorldVirtualEnvPy37>python main.py
    
    INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
    INFO: Started parent process [35788]
    
    Traceback (most recent call last):
    
      File "main.py", line 14, in <module>
        uvicorn.run(app, host='127.0.0.1', port=8000, workers=2)
    	
      File "C:\Users\marodev\source\repos\StarletteHelloWorldVirtualEnvPy37\venv\lib\site-packages\uvicorn\main.py", line 274, in run
        supervisor.run(server.run, sockets=[socket])
    	
      File "C:\Users\marodev\source\repos\StarletteHelloWorldVirtualEnvPy37\venv\lib\site-packages\uvicorn\supervisors\multiprocess.py", line 33, in run
        process.start()
    	
      File "C:\Users\marodev\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 112, in start
        self._popen = self._Popen(self)
    	
      File "C:\Users\marodev\AppData\Local\Programs\Python\Python37\lib\multiprocessing\context.py", line 223, in _Popen
        return _default_context.get_context().Process._Popen(process_obj)
    	
      File "C:\Users\marodev\AppData\Local\Programs\Python\Python37\lib\multiprocessing\context.py", line 322, in _Popen
        return Popen(process_obj)
    	
      File "C:\Users\marodev\AppData\Local\Programs\Python\Python37\lib\multiprocessing\popen_spawn_win32.py", line 65, in __init__
        reduction.dump(process_obj, to_child)
    	
      File "C:\Users\marodev\AppData\Local\Programs\Python\Python37\lib\multiprocessing\reduction.py", line 60, in dump
        ForkingPickler(file, protocol).dump(obj)
    	
    AttributeError: Can't pickle local object 'request_response.<locals>.app'
    
    (venv) C:\Users\marodev\source\repos\StarletteHelloWorldVirtualEnvPy37>Traceback (most recent call last):
      File "<string>", line 1, in <module>
      
      File "C:\Users\marodev\AppData\Local\Programs\Python\Python37\lib\multiprocessing\spawn.py", line 99, in spawn_main
        new_handle = reduction.steal_handle(parent_pid, pipe_handle)
    	
      File "C:\Users\marodev\AppData\Local\Programs\Python\Python37\lib\multiprocessing\reduction.py", line 82, in steal_handle
        _winapi.PROCESS_DUP_HANDLE, False, source_pid)
    	
    OSError: [WinError 87] The parameter is incorrect
    
    
    opened by marodev 38
  • Use optional package installs

    Use optional package installs

    This is #224 rebased to master. Closes #219.

    I also pinned wsproto because uvicorn does not work for wsproto 0.13. Would probably be good to fix that (in another PR).

    Suggestion: what about also adding [dev]. We could use that in the CI script, so we'd have all dependencies defined in a single place.

    opened by almarklein 36
  • RuntimeError: unable to perform operation on <TCPTransport

    RuntimeError: unable to perform operation on

    Checklist

    • [X] The bug is reproducible against the latest release and/or master.
    • [?] There are no similar issues or pull requests to fix it yet.

    Describe the bug

    We are seeing frequent RuntimeError: unable to perform operation on <TCPTransport closed=True reading=False 0x7f25014d3810>; the handler is closed

    To reproduce

    Run django with latest uvicorn inside kubernetes.

    Expected behavior

    No error

    Actual behavior

    Error

    Debugging material

    RuntimeError: unable to perform operation on <TCPTransport closed=True reading=False 0x55abf67bcd50>; the handler is closed
      File "websockets/legacy/server.py", line 232, in handler
        await self.close()
      File "websockets/legacy/protocol.py", line 779, in close
        await asyncio.shield(self.close_connection_task)
      File "websockets/legacy/protocol.py", line 1309, in close_connection
        self.transport.write_eof()
      File "uvloop/handles/stream.pyx", line 696, in uvloop.loop.UVStream.write_eof
      File "uvloop/handles/handle.pyx", line 159, in uvloop.loop.UVHandle._ensure_alive
    

    Environment

    • OS / Python / Uvicorn version: ubuntu / python 3.7 / uvicorn[standard]==0.15.0
    • The exact command you're running uvicorn with, all flags you passed included. If you run it with gunicorn please do the same. If there is a reverse-proxy involved and you cannot reproduce without it please give the minimal config of it to reproduce.

    Additional context

    This started happening when we started using uvicorn. In production it happens quite often:

    image

    There are some similar issues:

    • https://github.com/encode/uvicorn/pull/1140 - this is a issue with websockets on graceful shutdown. Considering the amount of exceptions, it seems like this is happening frequently, not just on shutdown
    • https://github.com/encode/uvicorn/issues/1229 - Someone ran into the same problem but decided to close it as an issue with websockets library. Given that the issue started occurring when we used uvicorn, I suspect an issue with uvicorn.
    opened by caleb15 33
  • Use optional package installs.

    Use optional package installs.

    Instead of the platform detection I’d like uvicorn to use optional installs.

    • pip install uvicorn - Just the package itself.
    • pip install uvicorn[standard] - uvloop/httptools/websockets
    • pip install uvicorn[pure] - asyncio/h11/wsproto
    • pip install uvicorn[full] - Everything
    opened by tomchristie 29
  • Switch from `watchgod` to `watchfiles`

    Switch from `watchgod` to `watchfiles`

    See https://watchfiles.helpmanual.io/migrating/.

    TL;DR; I've renamed watchgod to watchfiles and rewritten it to use the rust "notify" crate for all the heavy lifting. This PR is a WIP to see if you're willing to move ahead with a change like this.

    Still TODO/to answer:

    • I've changed the logic of BaseReload to try and make StatReload and watchfiles share an interface, are you happy with this?
    • tests are a fair bit more complicated, are you happy with how I'm implemented/changed them?
    • ~~I don't see an obvious way to deal with --reload-include and --reload-exclude being paths rather than extensions. Are you happy to drop this functionality? It's much less important now there's little overhead to scanning big directories, if not, we can implement something but it'll be complicated.~~ I think I've dealt with this a287d5c
    opened by samuelcolvin 28
  • High CPU usage when using --reload

    High CPU usage when using --reload

    Using the example code

    async def app(scope, receive, send):
        assert scope['type'] == 'http'
    
        await send({
            'type': 'http.response.start',
            'status': 200,
            'headers': [
                [b'content-type', b'text/plain'],
            ],
        })
        await send({
            'type': 'http.response.body',
            'body': b'Hello, world!',
        })
    

    and running uvicorn main:app, CPU usage is unnoticeable (less than 1% of 1 core). But when running with uvicorn main:app --reload, CPU usage jumps to 54% of 1 core.

    Environment: python -V: Python 3.7.2 uvicorn: 0.6.1 uname -a: Linux architect 5.0.4-arch1-1-ARCH #1 SMP PREEMPT Sat Mar 23 21:00:33 UTC 2019 x86_64 GNU/Linux

    opened by nymous 27
  • docker-compose graceful shutdown of uvicorn --reload

    docker-compose graceful shutdown of uvicorn --reload

    I'm trying to run uvicorn --reload in a docker-compose setup with a mounted directory of my code. When using "ctrl+c" and other ways to end the process it ends up just haning for the graceful shutdown periode and then it gets the SIGKILL because nothings happens. When looking at the strace it seems to be hanging indeed:

    wait4(7, 0x7ffcf1409d84, WNOHANG, NULL) = 0
    getpid()                                = 1
    write(2, "INFO: Stopping reloader process "..., 36) = 36
    getsockname(3, {sa_family=AF_INET, sin_port=htons(8000), sin_addr=inet_addr("0.0.0.0")}, [16]) = 0
    getpeername(3, 0x7ffcf1409f70, [16])    = -1 ENOTCONN (Transport endpoint is not connected)
    close(3)                                = 0
    wait4(7, 0x7ffcf140afe4, WNOHANG, NULL) = 0
    wait4(7, 0x7ffcf140afe4, WNOHANG, NULL) = 0
    getpid()                                = 1
    wait4(7,  <unfinished ...>)             = ?
    +++ killed by SIGKILL +++
    

    If I disable the reloader it shutdowns down fine. I've uploaded a minimal sample here: https://gist.github.com/sbv-trueenergy/a9a6971778a01d1acd3c466719e690b8

    Am I doing something wrong?

    opened by sbv-trueenergy 26
  • RuntimeError in uvicorn for some requests

    RuntimeError in uvicorn for some requests

    uvicorn==0.2.5 is throwing errors for some requests.

    ERROR: Exception in ASGI application
    Traceback (most recent call last):
      File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/uvicorn/protocols/http/httptools.py", line 196, in run_asgi
        result = await asgi(self.receive, self.send)
      File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/channels/http.py", line 190, in __call__
        await self.handle(body)
      File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/asgiref/sync.py", line 110, in __call__
        return await asyncio.wait_for(future, timeout=None)
      File "/usr/lib/python3.6/asyncio/tasks.py", line 339, in wait_for
        return (yield from fut)
      File "/usr/lib/python3.6/concurrent/futures/thread.py", line 56, in run
        result = self.fn(*self.args, **self.kwargs)
      File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/asgiref/sync.py", line 125, in thread_handler
        return self.func(*args, **kwargs)
      File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/channels/http.py", line 229, in handle
        self.send(response_message)
      File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/asgiref/sync.py", line 64, in __call__
        return call_result.result()
      File "/usr/lib/python3.6/concurrent/futures/_base.py", line 432, in result
        return self.__get_result()
      File "/usr/lib/python3.6/concurrent/futures/_base.py", line 384, in __get_result
        raise self._exception
      File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/asgiref/sync.py", line 78, in main_wrap
        result = await self.awaitable(*args, **kwargs)
      File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/uvicorn/protocols/http/httptools.py", line 308, in send
        protocol.transport.write(body)
      File "uvloop/handles/stream.pyx", line 636, in uvloop.loop.UVStream.write
      File "uvloop/handles/handle.pyx", line 165, in uvloop.loop.UVHandle._ensure_alive
    RuntimeError: unable to perform operation on <TCPTransport closed=True reading=False 0x1a48ad8>; the handler is closed
    
    bug need confirmation 
    opened by ChillarAnand 25
  • Shutdown process is broken in 0.15

    Shutdown process is broken in 0.15

    Checklist

    • [x] The bug is reproducible against the latest release and/or master.
    • [x] There are no similar issues or pull requests to fix it yet.

    Describe the bug

    My FastAPI ASGI server cannot shutdown properly with uvicorn==0.15 while it can with 0.14

    To reproduce

    Setup minimal FastAPI app and add some functions with logs(prints) to shutdown event

    Expected behavior

    You see all logs(prints) from functions on shutdown

    Actual behavior

    Get ASGI 'lifespan' protocol appears unsupported. without --lifespan on Get error trace with --lifespan on

    Debugging material

    uvicorn scheduler.main:app --host=0.0.0.0 --port ${WEB_PORT:-8000} --reload --lifespan on INFO: Will watch for changes in these directories: ['/home/dmytro/storage/chimplie/projects/raok-main/raok-scheduler'] INFO: Uvicorn running on http://0.0.0.0:8004 (Press CTRL+C to quit) INFO: Started reloader process [177653] using statreload INFO: Started server process [177655] INFO: Waiting for application startup. INFO: Tortoise-ORM started, {'default': <tortoise.backends.asyncpg.client.AsyncpgDBClient object at 0x7f63d4a10e50>}, {'models': {'Task': <class 'scheduler.models.task.Task'>, 'Aerich': <class 'aerich.models.Aerich'>}} INFO: Application startup complete. ^CINFO: Shutting down INFO: Finished server process [177655] ERROR: Exception in 'lifespan' protocol Traceback (most recent call last): File "/home/dmytro/.local/share/virtualenvs/raok-scheduler-hpGGYNLi/lib/python3.8/site-packages/uvicorn/lifespan/on.py", line 84, in main await app(scope, self.receive, self.send) File "/home/dmytro/.local/share/virtualenvs/raok-scheduler-hpGGYNLi/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in call return await self.app(scope, receive, send) File "/home/dmytro/.local/share/virtualenvs/raok-scheduler-hpGGYNLi/lib/python3.8/site-packages/fastapi/applications.py", line 199, in call await super().call(scope, receive, send) File "/home/dmytro/.local/share/virtualenvs/raok-scheduler-hpGGYNLi/lib/python3.8/site-packages/starlette/applications.py", line 112, in call await self.middleware_stack(scope, receive, send) File "/home/dmytro/.local/share/virtualenvs/raok-scheduler-hpGGYNLi/lib/python3.8/site-packages/starlette/middleware/errors.py", line 146, in call await self.app(scope, receive, send) File "/home/dmytro/.local/share/virtualenvs/raok-scheduler-hpGGYNLi/lib/python3.8/site-packages/starlette/middleware/cors.py", line 70, in call await self.app(scope, receive, send) File "/home/dmytro/.local/share/virtualenvs/raok-scheduler-hpGGYNLi/lib/python3.8/site-packages/starlette/exceptions.py", line 58, in call await self.app(scope, receive, send) File "/home/dmytro/.local/share/virtualenvs/raok-scheduler-hpGGYNLi/lib/python3.8/site-packages/starlette/routing.py", line 569, in call await self.lifespan(scope, receive, send) File "/home/dmytro/.local/share/virtualenvs/raok-scheduler-hpGGYNLi/lib/python3.8/site-packages/starlette/routing.py", line 544, in lifespan await receive() File "/home/dmytro/.local/share/virtualenvs/raok-scheduler-hpGGYNLi/lib/python3.8/site-packages/uvicorn/lifespan/on.py", line 135, in receive return await self.receive_queue.get() File "/usr/lib64/python3.8/asyncio/queues.py", line 163, in get await getter asyncio.exceptions.CancelledError INFO: Stopping reloader process [177653]

    image

    Environment

    • Fedora 34 / Python 3.8 / Uvicorn version: 0.15 - bug, 0.14 - ok
    • command to run: uvicorn main:app --host=0.0.0.0 --port 8000 --reload
    bug 
    opened by KharchenkoDmitriy 24
  • Bump httpx from 0.23.0 to 0.23.1

    Bump httpx from 0.23.0 to 0.23.1

    Bumps httpx from 0.23.0 to 0.23.1.

    Release notes

    Sourced from httpx's releases.

    Version 0.23.1

    0.23.1

    Added

    Fixed

    • Don't drop empty query parameters. (#2354)

    Removed

    • Drop .read/.aread from SyncByteStream/AsyncByteStream (#2407)
    • Drop RawURL. (#2241)
    Changelog

    Sourced from httpx's changelog.

    0.23.1

    Note: The 0.23.1 release should have used a proper version bump, rather than a minor point release. There are API surface area changes that may affect some users. See the "Removed" section of these release notes for details.

    Added

    Fixed

    • Don't drop empty query parameters. (#2354)

    Removed

    • Upload files must always be opened in binary mode. (#2400)
    • Drop .read/.aread from SyncByteStream/AsyncByteStream. (#2407)
    • Drop RawURL. (#2241)
    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    dependencies python 
    opened by dependabot[bot] 0
  • Bump asgiref from 3.5.2 to 3.6.0

    Bump asgiref from 3.5.2 to 3.6.0

    Bumps asgiref from 3.5.2 to 3.6.0.

    Changelog

    Sourced from asgiref's changelog.

    3.6.0 (2022-12-20)

    • Two new functions are added to the asgiref.sync module: iscoroutinefunction() and markcoroutinefunction().

      Python 3.12 deprecates asyncio.iscoroutinefunction() as an alias for inspect.iscoroutinefunction(), whilst also removing the _is_coroutine marker. The latter is replaced with the inspect.markcoroutinefunction decorator.

      The new asgiref.sync functions are compatibility shims for these functions that can be used until Python 3.12 is the minimum supported version.

      Note that these functions are considered beta, and as such, whilst not likely, are subject to change in a point release, until the final release of Python 3.12. They are included in asgiref now so that they can be adopted by Django 4.2, in preparation for support of Python 3.12.

    • The loop argument to asgiref.timeout.timeout is deprecated. As per other asyncio based APIs, the running event loop is used by default. Note that asyncio provides timeout utilities from Python 3.11, and these should be preferred where available.

    • Support for the ASGI_THREADS environment variable, used by SyncToAsync, is removed. In general, a running event-loop is not available to asgiref at import time, and so the default thread pool executor cannot be configured. Protocol servers, or applications, should set the default executor as required when configuring the event loop at application startup.

    Commits
    • 79a1d01 Releasing 3.6.0.
    • 7a6c631 Added release note for coroutine detection shims.
    • bea951e Added Python 3.11 support
    • ea79016 Updated timeout helper to use get_running_loop(). (#337)
    • 36f37c9 Added coroutine detection shims for Python 3.12 (#360)
    • 467c154 Updated pre-commit versions and fixed linting errors.
    • 72c5ca1 Updated flake8 repo location in pre-commit config to github.
    • 8b76e05 Tweak conf.py to fix Sphinx warnings (#355)
    • 9acb1a2 Added missing HTTPResponseTrailersEvent to ASGISendEvent
    • 3b5aaff Added HTTP Trailers extension
    • Additional commits viewable in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    dependencies python 
    opened by dependabot[bot] 0
  • Add `--timeout-graceful-shutdown` parameter

    Add `--timeout-graceful-shutdown` parameter

    • Closes https://github.com/encode/uvicorn/issues/451
    • Closes https://github.com/encode/uvicorn/issues/675

    Description

    This PR adds the --timeout-graceful-shutdown parameter, which sets a timeout on tasks that the server is waiting to perform the shutdown lifespan event.

    This timeout doesn't act on the shutdown lifespan event i.e. the shutdown event is supposed to run even in the scenario the tasks are timed out.

    I need to think of a nice test for this. Ideas?

    Notes

    1. The --graceful-timeout from gunicorn is conceptually different from --timeout-graceful-shutdown. The first forcefully kills the worker, and the latter cancels the tasks that are running after uvicorn said "I'm not receiving more requests, I'm just going to finish some tasks".

    2. An alternative name for this parameter could be --timeout-tasks-shutdown, or similar.

    3. The logs are horrible when a task is cancelled. Should it be fine tho? :shrug:

    Missing

    • [ ] Add an entry on the documentation.
    • [ ] Add a test case.
    opened by Kludex 0
  • Send code 1012 on shutdown for websockets

    Send code 1012 on shutdown for websockets

    • Closes #1344

    Changes

    • Send 500 or 1012 to the client on server shutdown, instead of the previous 1006.
    • Improve test suite to check what the client receives, instead of only the ASGI application.
    • Increase coverage from 98.5% to 98.8%.
    opened by Kludex 1
Releases(0.20.0)
  • 0.20.0(Nov 20, 2022)

    Added

    • Check if handshake is completed before sending frame on wsproto shutdown (#1737)
    • Add default headers to WebSockets implementations (#1606 & #1747) 28/10/22
    • Warn user when reload and workers flag are used together (#1731) 31/10/22

    Fixed

    • Use correct WebSocket error codes on close (#1753) 20/11/22
    • Send disconnect event on connection lost for wsproto (#996) 29/10/22
    • Add SIGQUIT handler to UvicornWorker (#1710) 01/11/22
    • Fix crash on exist with "--uds" if socket doesn't exist (#1725) 27/10/22
    • Annotate CONFIG_KWARGS in UvicornWorker class (#1746) 31/10/22

    Removed

    • Remove conditional on RemoteProtocolError.event_hint on wsproto (#1486) 31/10/22
    • Remove unused handle_no_connect on wsproto implementation (#1759) 17/11/22
    Source code(tar.gz)
    Source code(zip)
  • 0.19.0(Oct 19, 2022)

    Added

    • Support Python 3.11 (#1652) 16/09/22
    • Bump minimal httptools version to 0.5.0 (#1645) 13/09/22
    • Ignore HTTP/2 upgrade and optionally ignore WebSocket upgrade (#1661) 19/10/22
    • Add py.typed to comply with PEP 561 (#1687) 07/10/22

    Fixed

    • Set propagate to False on "uvicorn" logger (#1288) 08/10/22
    • USR1 signal is now handled correctly on UvicornWorker. (#1565) 26/08/22
    • Use path with query string on WebSockets logs (#1385) 11/09/22
    • Fix behavior on which "Date" headers were the same per connection (#1706) 19/10/22

    Removed

    • Remove the --debug flag (#1640) 14/09/22
    • Remove the DebugMiddleware (#1697) 07/10/22
    Source code(tar.gz)
    Source code(zip)
  • 0.18.3(Aug 24, 2022)

    What's Changed

    • Remove cyclic references on HTTP implementations. (#1604) 24/08/22
    • reload_delay default changed from None to 0.25 on uvicorn.run() and Config. None is not an acceptable value anymore. (#1545) 02/07/22

    Full Changelog: https://github.com/encode/uvicorn/compare/0.18.2...0.18.3

    Source code(tar.gz)
    Source code(zip)
  • 0.18.2(Jun 27, 2022)

    What's Changed

    • Add default log_config on uvicorn.run() #1541 24/06/22
    • Revert logging file name modification #1543 27/06/22

    Full Changelog: https://github.com/encode/uvicorn/compare/0.18.1...0.18.2

    Source code(tar.gz)
    Source code(zip)
  • 0.18.1(Jun 23, 2022)

    Fixed

    • Use DEFAULT_MAX_INCOMPLETE_EVENT_SIZE as default to h11_max_incomplete_event_size on the CLI (#1534) 23/06/22

    Full Changelog: https://github.com/encode/uvicorn/compare/0.18.0...0.18.1

    Source code(tar.gz)
    Source code(zip)
  • 0.18.0(Jun 23, 2022)

    Added

    • The reload flag prioritizes watchfiles instead of the deprecated watchgod (#1437) 18/06/22
    • Annotate uvicorn.run() function (#1423) 10/05/22
    • Allow configuring max_incomplete_event_size for h11 implementation (#1514) 22/06/22

    Removed

    • Remove asgiref dependency (#1532) 22/06/22

    Fixed

    • Turn raw_path into bytes on both websockets implementations (#1487) 16/05/22
    • Revert log exception traceback in case of invalid HTTP request (#1518) 14/06/22
    • Set asyncio.WindowsSelectorEventLoopPolicy() when using multiple workers to avoid "WinError 87" (#1454) 22/06/22

    Full Changelog: https://github.com/encode/uvicorn/compare/0.17.6...0.18.0

    Source code(tar.gz)
    Source code(zip)
  • 0.17.6(Mar 11, 2022)

  • 0.17.5(Feb 16, 2022)

    0.17.5 - 2022-02-16

    Fixed

    • Fix case where url is fragmented in httptools protocol (#1263) 2/16/22
    • Fix WSGI middleware not to explode quadratically in the case of a larger body (#1329) 2/16/16

    Changed

    • Send HTTP 400 response for invalid request (#1352) 2/11/22

    Full Changelog: https://github.com/encode/uvicorn/compare/0.17.4...0.17.5

    Source code(tar.gz)
    Source code(zip)
  • 0.17.4(Feb 4, 2022)

  • 0.17.3(Feb 3, 2022)

  • 0.17.2(Feb 3, 2022)

    Fixed

    • Revert #1332. While trying to solve the memory leak, it introduced an issue (#1345) when the server receives big chunks of data using the httptools implementation. (#1354) 03/02/22
    • Revert stream interface changes. This was introduced on 0.14.0, and caused an issue (#1226), which caused a memory leak when sending TCP pings. (#1355) 03/02/22
    • Fix wsproto version check expression (#1342) 28/01/22
    Source code(tar.gz)
    Source code(zip)
  • 0.17.1(Jan 28, 2022)

    Fixed

    • Move all data handling logic to protocol and ensure connection is closed. (#1332) 28/01/22
    • Change spec_version field from "2.1" to "2.3", as Uvicorn is compliant with that version of the ASGI specifications. (#1337) 25/01/22
    Source code(tar.gz)
    Source code(zip)
  • 0.17.0.post1(Jan 24, 2022)

  • 0.17.0(Jan 14, 2022)

    Added

    • Allow configurable websocket per-message-deflate setting (#1300) 29/12/21
    • Support extra_headers for WS accept message (#1293) 06/01/22
    • Add missing http version on websockets scope (#1309) 08/01/22

    Fixed/Removed

    • Drop Python 3.6 support (#1261) 06/01/22
    • Fix reload process behavior when exception is raised (#1313) 11/01/22
    • Remove root_path from logs (#1294) 25/12/21
    Source code(tar.gz)
    Source code(zip)
  • 0.16.0(Dec 8, 2021)

    0.16.0 - 2021-12-08

    Added

    • Enable read of uvicorn settings from environment variables (#1279) 06/12/21
    • Bump websockets to 10.0. (#1180) 13/09/21
    • Ensure non-zero exit code when startup fails (#1278) 06/12/21
    • Increase httptools version range from "==0.2.*" to ">=0.2.0,<0.4.0". (#1243) 8/11/21
    • Override default asyncio event loop with reload only on Windows (#1257) 24/11/21
    • Replace HttpToolsProtocol.pipeline type from list to deque. (#1213) 10/10/21
    • Replace WSGIResponder.send_queue type from list to deque. (#1214) 10/10/21

    Fixed

    • Main process exit after startup failure on reloader classes (#1177) 30/09/21
    • Add explicit casting on click options (#1217) 11/10/21
    • Allow WebSocket close event to receive reason being None from ASGI app. (#1259) 23/11/21
    • Fix a bug in WebSocketProtocol.asgi_receive on which we returned a close frame even if there were data messages before that frame in the read queue. (#1252) 25/11/21
    • The option --reload-dirs was splitting a string into single character directories. (#1267) 25/11/21
    • Only second SIGINT is able to forcelly shutdown the server (#1269) 28/11/21
    • Allow app-dir parameter on the run() function (#1271) 06/12/21
    Source code(tar.gz)
    Source code(zip)
  • 0.15.0(Aug 13, 2021)

    0.15.0 - 2021-08-13

    Added

    • Change reload to be configurable with glob patterns. Currently only .py files are watched, which is different from the previous default behavior. (#820) 08/08/21
    • Add Python 3.10-rc.1 support. Now the server uses asyncio.run which will: start a fresh asyncio event loop, on shutdown cancel any background tasks rather than aborting them, aexit any remaining async generators, and shutdown the default ThreadPoolExecutor. (#1070) 30/07/21
    • Exit with status 3 when worker starts failed (#1077) 22/06/21
    • Add option to set websocket ping interval and timeout (#1048) 09/06/21
    • Adapt bind_socket to make it usable with multiple processes (#1009) 21/06/21
    • Add existence check to the reload directory(ies) (#1089) 21/06/21
    • Add missing trace log for websocket protocols (#1083) 19/06/21
    • Support disabling default Server and Date headers (#818) 11/06/21

    Changed

    • Add PEP440 compliant version of click (#1099) 29/06/21
    • Bump asgiref to 3.4.0 (#1100) 29/06/21

    Fixed

    • When receiving a SIGTERM supervisors now terminate their processes before joining them (#1069) 30/07/21
    • Fix the need of httptools on minimal installation (#1135) 30/07/21
    • Fix ping parameters annotation in Config class (#1127) 19/07/21
    Source code(tar.gz)
    Source code(zip)
  • 0.14.0(Jun 1, 2021)

    0.14.0 - 2021-06-01

    Added

    • Defaults ws max_size on server to 16MB (#995) 5/29/21
    • Improve user feedback if no ws library installed (#926 and #1023) 2/27/21
    • Support 'reason' field in 'websocket.close' messages (#957) 2/24/21
    • Implemented lifespan.shutdown.failed (#755) 2/25/21

    Changed

    • Upgraded websockets requirements (#1065) 6/1/21
    • Switch to asyncio streams API (#869) 5/29/21
    • Update httptools from 0.1.* to 0.2.* (#1024) 5/28/21
    • Allow Click 8.0, refs #1016 (#1042) 5/23/21
    • Add search for a trusted host in ProxyHeadersMiddleware (#591) 3/13/21
    • Up wsproto to 1.0.0 (#892) 2/25/21

    Fixed

    • Force reload_dirs to be a list (#978) 6/1/21
    • Fix gunicorn worker not running if extras not installed (#901) 5/28/21
    • Fix socket port 0 (#975) 3/5/21
    • Prevent garbage collection of main lifespan task (#972) 3/4/21
    Source code(tar.gz)
    Source code(zip)
  • 0.13.4(Feb 20, 2021)

    0.13.4 - 2021-02-20

    Fixed

    • Fixed wsgi middleware PATH_INFO encoding (#962) 2/20/21
    • Fixed uvloop dependency (#952) 2/10/21 then (#959) 2/20/21
    • Relax watchgod up bound (#946) 1/31/21
    • Return 'connection: close' header in response (#721) 1/25/21

    Added:

    • Docs: Nginx + websockets (#948) 2/10/21
    • Document the default value of 1 for workers (#940) (#943) 1/25/21
    • Enabled permessage-deflate extension in websockets (#764) 1/1/21
    Source code(tar.gz)
    Source code(zip)
  • 0.13.3(Dec 29, 2020)

    0.13.3 - 2020-12-29

    Fixed

    • Prevent swallowing of return codes from subprocess when running with Gunicorn by properly resetting signals. (Pull #895)
    • Tweak detection of app factories to be more robust. A warning is now logged when passing a factory without the --factory flag. (Pull #914)
    • Properly clean tasks when handshake is aborted when running with --ws websockets. (Pull #921)
    Source code(tar.gz)
    Source code(zip)
  • 0.13.2(Dec 20, 2020)

  • 0.13.1(Dec 12, 2020)

    0.13.1 - 2020-12-12

    Fixed

    • Prevent exceptions when the ASGI application rejects a connection during the WebSocket handshake, when running on both --ws wsproto or --ws websockets. (Pull #704 and #881)
    • Ensure connection scope doesn't leak in logs when using JSON log formatters. (Pull #859 and #884)
    Source code(tar.gz)
    Source code(zip)
  • 0.13.0(Dec 8, 2020)

    0.13.0 - 2020-12-08

    Added

    • Add --factory flag to support factory-style application imports. (#875) 2020-12-07 50fc0d1c
    • Skip installation of signal handlers when not in the main thread. Allows using Server in multithreaded contexts without having to override .install_signal_handlers(). (#871) 2020-12-07 ce2ef45a
    Source code(tar.gz)
    Source code(zip)
  • 0.12.3(Nov 22, 2020)

    0.12.3 - 2020-11-21

    Fixed

    • Fix race condition that leads Quart to hang with uvicorn (#848) 11/18/20 de213614
    • Use latin1 when decoding X-Forwarded-* headers (#701) 11/12/20 45e6e831
    • Rework IPv6 support (#837) 11/8/20 bdab488e
    • Cancel old keepalive-trigger before setting new one. (#832) 10/26/20 d5dcf80c
    Source code(tar.gz)
    Source code(zip)
  • 0.12.2(Oct 19, 2020)

    0.12.2 - 2020-10-19

    Added

    • Adding ability to decrypt ssl key file (#808) 10/12/20 90dbb6e0
    • Support .yml log config files (#799) 10/6/20 b468950e
    • Added python 3.9 support (#804) 10/6/20 08fd0559

    Fixed

    • Fixes watchgod with common prefixes (#817) 10/14/20 1b32f997
    • Fix reload with ipv6 host (#803) 10/14/20 5acaee5b
    • Added cli suport for headers containing colon (#813) 10/12/20 68732899
    • Sharing socket across workers on windows (#802) 10/12/20 103167a0
    • Note the need to configure trusted "ips" when using unix sockets (#796) 10/4/20 a504c569
    Source code(tar.gz)
    Source code(zip)
  • 0.12.1(Sep 30, 2020)

    0.12.1 - 2020-09-30

    Changed

    • Pinning h11 and python-dotenv to min versions (#789) 9/29/20 bbf19c66
    • Get docs/index.md in sync with README.md (#784) 9/29/20 70ebcfdf

    Fixed

    • Improve changelog by pointing out breaking changes (#792) 9/29/20 e2b75064
    Source code(tar.gz)
    Source code(zip)
  • 0.12.0(Sep 28, 2020)

    0.12.0 - 2020-09-28

    Added

    • Make reload delay configurable (#774) 9/28/20 98010027
    • Upgrade maximum h11 dependency version to 0.10 (#772) 8/28/20 54d729cc
    • Allow .json or .yaml --log-config files (#665) 8/18/20 093a1f7c
    • Add ASGI dict to the lifespan scope (#754) 8/15/20 8150c3eb
    • Upgrade wsproto to 0.15.0 (#750) 8/13/20 fbce393f
    • Use optional package installs (#666) 8/10/20 5fa99a11

    Changed

    • Dont set log level for root logger (#767) 8/28/20 df81b168

    Fixed

    • Revert "Improve shutdown robustness when using --reload or multiprocessing (#620)" (#756) 8/28/20 ff4af12d
    • Fix terminate error in windows (#744) 8/27/20 dd3b842d
    • Fix bug where --log-config disables uvicorn loggers (#512) 8/11/20 a9c37cc4
    Source code(tar.gz)
    Source code(zip)
  • 0.11.8(Jul 31, 2020)

    0.11.8 - 2020-07-30

    • Fix a regression that caused Uvicorn to crash when using --interface=wsgi. (Pull #730)
    • Fix a regression that caused Uvicorn to crash when using unix domain sockets. (Pull #729)
    Source code(tar.gz)
    Source code(zip)
  • 0.11.7(Jul 28, 2020)

    0.11.7

    • SECURITY FIX: Prevent sending invalid HTTP header names and values.
    • SECURITY FIX: Ensure path value is escaped before logging to the console.
    Source code(tar.gz)
    Source code(zip)
  • 0.11.6(Jul 17, 2020)

  • 0.11.5(Apr 29, 2020)

    • Revert "Watch all files, not just .py" due to unexpected side effects.
    • Revert "Pass through gunicorn timeout config." due to unexpected side effects.
    Source code(tar.gz)
    Source code(zip)
Owner
Encode
Collaboratively funded software development.
Encode
The little ASGI framework that shines. ?

✨ The little ASGI framework that shines. ✨ Documentation: https://www.starlette.io/ Community: https://discuss.encode.io/c/starlette Starlette Starlet

Encode 7.7k Jan 1, 2023
Bablyon 🐍 A small ASGI web framework

A small ASGI web framework that you can make asynchronous web applications using uvicorn with using few lines of code

xArty 8 Dec 7, 2021
FPS, fast pluggable server, is a framework designed to compose and run a web-server based on plugins.

FPS, fast pluggable server, is a framework designed to compose and run a web-server based on plugins. It is based on top of fastAPI, uvicorn, typer, and pluggy.

Adrien Delsalle 1 Nov 16, 2021
Async Python 3.6+ web server/framework | Build fast. Run fast.

Sanic | Build fast. Run fast. Build Docs Package Support Stats Sanic is a Python 3.6+ web server and web framework that's written to go fast. It allow

Sanic Community Organization 16.7k Jan 8, 2023
Async Python 3.6+ web server/framework | Build fast. Run fast.

Sanic | Build fast. Run fast. Build Docs Package Support Stats Sanic is a Python 3.6+ web server and web framework that's written to go fast. It allow

Sanic Community Organization 16.7k Dec 28, 2022
Screaming-fast Python 3.5+ HTTP toolkit integrated with pipelining HTTP server based on uvloop and picohttpparser.

Japronto! There is no new project development happening at the moment, but it's not abandoned either. Pull requests and new maintainers are welcome. I

Paweł Piotr Przeradowski 8.6k Dec 29, 2022
Asynchronous HTTP client/server framework for asyncio and Python

Async http client/server framework Key Features Supports both client and server side of HTTP protocol. Supports both client and server Web-Sockets out

aio-libs 13.2k Jan 5, 2023
Fast, asynchronous and elegant Python web framework.

Warning: This project is being completely re-written. If you're curious about the progress, reach me on Slack. Vibora is a fast, asynchronous and eleg

vibora.io 5.7k Jan 8, 2023
FastAPI framework, high performance, easy to learn, fast to code, ready for production

FastAPI framework, high performance, easy to learn, fast to code, ready for production Documentation: https://fastapi.tiangolo.com Source Code: https:

Sebastián Ramírez 53k Jan 2, 2023
bottle.py is a fast and simple micro-framework for python web-applications.

Bottle: Python Web Framework Bottle is a fast, simple and lightweight WSGI micro web-framework for Python. It is distributed as a single file module a

Bottle Micro Web Framework 7.8k Dec 31, 2022
Fully featured framework for fast, easy and documented API development with Flask

Flask RestPlus IMPORTANT NOTICE: This project has been forked to Flask-RESTX and will be maintained by by the python-restx organization. Flask-RESTPlu

Axel H. 2.7k Jan 4, 2023
Fully featured framework for fast, easy and documented API development with Flask

Flask RestPlus IMPORTANT NOTICE: This project has been forked to Flask-RESTX and will be maintained by by the python-restx organization. Flask-RESTPlu

Axel H. 2.5k Feb 17, 2021
Django Ninja - Fast Django REST Framework

Django Ninja is a web framework for building APIs with Django and Python 3.6+ type hints.

Vitaliy Kucheryaviy 3.8k Jan 2, 2023
Appier is an object-oriented Python web framework built for super fast app development.

Joyful Python Web App development Appier is an object-oriented Python web framework built for super fast app development. It's as lightweight as possi

Hive Solutions 122 Dec 22, 2022
A minimal, extensible, fast and productive API framework for Python 3.

molten A minimal, extensible, fast and productive API framework for Python 3. Changelog: https://moltenframework.com/changelog.html Community: https:/

Bogdan Popa 980 Nov 28, 2022
Bionic is Python Framework for crafting beautiful, fast user experiences for web and is free and open source

Bionic is fast. It's powered core python without any extra dependencies. Bionic offers stateful hot reload, allowing you to make changes to your code and see the results instantly without restarting your app or losing its state.

 ⚓ 0 Mar 5, 2022
TinyAPI - 🔹 A fast & easy and lightweight WSGI Framework for Python

TinyAPI - ?? A fast & easy and lightweight WSGI Framework for Python

xArty 3 Apr 8, 2022
The lightning-fast ASGI server. 🦄

The lightning-fast ASGI server. Documentation: https://www.uvicorn.org Community: https://discuss.encode.io/c/uvicorn Requirements: Python 3.6+ (For P

Encode 6k Jan 3, 2023
asgi-server-timing-middleware

ASGI Server-Timing middleware An ASGI middleware that wraps the excellent yappi profiler to let you measure the execution time of any function or coro

null 33 Dec 15, 2022
Fast⚡, simple and light💡weight ASGI micro🔬 web🌏-framework for Python🐍.

NanoASGI Asynchronous Python Web Framework NanoASGI is a fast ⚡ , simple and light ?? weight ASGI micro ?? web ?? -framework for Python ?? . It is dis

Kavindu Santhusa 8 Jun 16, 2022