How to use uvicorn - 10 common examples

To help you get started, we’ve selected a few uvicorn examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pytest-docker-compose / pytest-docker-compose / tests / pytest_docker_compose_tests / my_network / a_buildable_container / an_api.py View on Github external
return cursor.fetchone()


@app.delete("/items/{item_id}")
def delete_item(item_id: int):
    with CONNECTION.cursor() as cursor:
        cursor.execute('DELETE FROM my_table WHERE num=%s RETURNING *;', (item_id, ))
        CONNECTION.commit()
        return cursor.fetchone()


if __name__ == "__main__":
    try:
        CONNECTION = psycopg2.connect(dbname='postgres', user='postgres', host='my_db', port=5432)
        create_database()
        uvicorn.run(app, host="0.0.0.0", port=5000, log_level="info")
    finally:
        CONNECTION.close()
github encode / uvicorn / tests / test_lifespan.py View on Github external
async def test():
        config = Config(app=app, lifespan="auto")
        lifespan = LifespanOn(config)

        assert not startup_complete
        assert not shutdown_complete
        await lifespan.startup()
        assert startup_complete
        assert not shutdown_complete
        await lifespan.shutdown()
        assert startup_complete
        assert shutdown_complete
github encode / uvicorn / tests / test_lifespan.py View on Github external
async def test():
        config = Config(app=app, lifespan="on")
        lifespan = LifespanOn(config)

        assert not startup_complete
        assert not shutdown_complete
        await lifespan.startup()
        assert startup_complete
        assert not shutdown_complete
        await lifespan.shutdown()
        assert startup_complete
        assert shutdown_complete
github encode / uvicorn / tests / test_lifespan.py View on Github external
async def test():
        config = Config(app=app, lifespan="auto")
        lifespan = LifespanOn(config)

        await lifespan.startup()
        assert lifespan.error_occured
        assert not lifespan.should_exit
        await lifespan.shutdown()
github encode / uvicorn / tests / test_lifespan.py View on Github external
async def test():
        config = Config(app=app, lifespan="on")
        lifespan = LifespanOn(config)

        assert not startup_complete
        assert not shutdown_complete
        await lifespan.startup()
        assert startup_complete
        assert not shutdown_complete
        await lifespan.shutdown()
        assert startup_complete
        assert shutdown_complete
github encode / uvicorn / tests / test_lifespan.py View on Github external
async def test():
        config = Config(app=app, lifespan="on")
        lifespan = LifespanOn(config)

        await lifespan.startup()
        assert lifespan.error_occured
        assert lifespan.should_exit
        await lifespan.shutdown()
github encode / uvicorn / tests / test_lifespan.py View on Github external
async def test():
        config = Config(app=app, lifespan="auto")
        lifespan = LifespanOn(config)

        assert not startup_complete
        assert not shutdown_complete
        await lifespan.startup()
        assert startup_complete
        assert not shutdown_complete
        await lifespan.shutdown()
        assert startup_complete
        assert shutdown_complete
github encode / uvicorn / tests / test_config.py View on Github external
def test_ssl_config(certfile_and_keyfile):
    certfile, keyfile = certfile_and_keyfile
    config = Config(app=asgi_app, ssl_certfile=certfile, ssl_keyfile=keyfile)
    config.load()

    assert config.is_ssl is True
github encode / uvicorn / tests / test_config.py View on Github external
def test_wsgi_app():
    config = Config(app=wsgi_app, interface="wsgi", proxy_headers=False)
    config.load()

    assert isinstance(config.loaded_app, WSGIMiddleware)
    assert config.interface == "wsgi"
github encode / uvicorn / tests / test_main.py View on Github external
def test_run():
    class App:
        def __init__(self, scope):
            if scope["type"] != "http":
                raise Exception()

        async def __call__(self, receive, send):
            await send({"type": "http.response.start", "status": 204, "headers": []})
            await send({"type": "http.response.body", "body": b"", "more_body": False})

    class CustomServer(Server):
        def install_signal_handlers(self):
            pass

    config = Config(app=App, loop="asyncio", limit_max_requests=1)
    server = CustomServer(config=config)
    thread = threading.Thread(target=server.run)
    thread.start()
    while not server.started:
        time.sleep(0.01)
    response = requests.get("http://127.0.0.1:8000")
    assert response.status_code == 204
    thread.join()