pythondockeruvicornfletasgi

Deploying Flet app from a docker container using Uvicorn


I am trying to run my flet as a web app from a docker container, but I am getting the following error message:

RuntimeError: asyncio.run() cannot be called from a running event loop
2024-05-27T18:46:46.450774857Z sys:1: RuntimeWarning: coroutine 'app_async' was never awaited

I have tried starting the app with and without exposing asgi interface:

def run_web():
    ft.app(target=main, export_asgi_app=True)

My entrypoint script looks like this:

#!/usr/bin/env bash

source .venv/bin/activate
uvicorn  --factory reggui.main:run_web --host 0.0.0.0 --port 8060

Any hints about what I may be missing?

UPDATE:

It seems that with uvicorn the entry function (in this case, main) needs to be an async function.

Now the app starts, but now there is another error which maybe related to Docker, but I am not sure:

INFO:     Will watch for changes in these directories: ['/']
2024-05-28T11:57:22.142940833Z INFO:     Uvicorn running on http://0.0.0.0:8060 (Press CTRL+C to quit)
2024-05-28T11:57:22.142967464Z INFO:     Started reloader process [8] using WatchFiles
2024-05-28T11:57:22.899964258Z WARNING:  ASGI app factory detected. Using it, but please consider setting the --factory flag explicitly.
2024-05-28T11:57:22.900006979Z INFO:     Started server process [10]
2024-05-28T11:57:22.900026356Z INFO:     Waiting for application startup.
2024-05-28T11:57:22.900085509Z INFO:     ASGI 'lifespan' protocol appears unsupported.
2024-05-28T11:57:22.900107430Z INFO:     Application startup complete.
2024-05-28T11:57:53.724415871Z ERROR:    Exception in ASGI application
2024-05-28T11:57:53.724433625Z Traceback (most recent call last):
2024-05-28T11:57:53.724436010Z   File "/.venv/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 411, in run_asgi
2024-05-28T11:57:53.724438114Z     result = await app(  # type: ignore[func-returns-value]
2024-05-28T11:57:53.724439907Z              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-28T11:57:53.724441671Z   File "/.venv/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 69, in __call__
2024-05-28T11:57:53.724443514Z     return await self.app(scope, receive, send)
2024-05-28T11:57:53.724445257Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-28T11:57:53.724447001Z   File "/.venv/lib/python3.11/site-packages/uvicorn/middleware/asgi2.py", line 14, in __call__
2024-05-28T11:57:53.724448804Z     instance = self.app(scope)
2024-05-28T11:57:53.724450548Z                ^^^^^^^^^^^^^^^
2024-05-28T11:57:53.724452281Z TypeError: 'NoneType' object is not callable
2024-05-28T11:57:53.724445979Z INFO:     172.20.0.1:34188 - "GET / HTTP/1.1" 500 Internal Server Error
2024-05-28T11:57:53.745830886Z ERROR:    Exception in ASGI application
2024-05-28T11:57:53.745845213Z Traceback (most recent call last):
2024-05-28T11:57:53.745847708Z   File "/.venv/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 411, in run_asgi
2024-05-28T11:57:53.745849792Z     result = await app(  # type: ignore[func-returns-value]
2024-05-28T11:57:53.745851786Z              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-28T11:57:53.745853689Z   File "/.venv/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 69, in __call__
2024-05-28T11:57:53.745852287Z INFO:     172.20.0.1:34198 - "GET /favicon.ico HTTP/1.1" 500 Internal Server Error
2024-05-28T11:57:53.745855603Z     return await self.app(scope, receive, send)
2024-05-28T11:57:53.745871904Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-28T11:57:53.745874589Z   File "/.venv/lib/python3.11/site-packages/uvicorn/middleware/asgi2.py", line 14, in __call__
2024-05-28T11:57:53.745882965Z     instance = self.app(scope)
2024-05-28T11:57:53.745884188Z                ^^^^^^^^^^^^^^^
2024-05-28T11:57:53.745885290Z TypeError: 'NoneType' object is not callable

Solution

  • This is the best solution I found: Using Hypercorn instead of Uvicorn

    So I changed the entrypoint script to:

    #!/usr/bin/env bash
    
    source .venv/bin/activate
    hypercorn reggui.main:app --reload --bind 0.0.0.0:8060
    

    But I had to also move the app to the global scope:

    app = ft.app(target=main, export_asgi_app=True)
    

    Instead of creating it inside the run function.