Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

running the app from uvicorns Python API does not work #3

Open
pmeier opened this issue Apr 25, 2024 · 5 comments
Open

running the app from uvicorns Python API does not work #3

pmeier opened this issue Apr 25, 2024 · 5 comments

Comments

@pmeier
Copy link
Collaborator

pmeier commented Apr 25, 2024

uvicorn test:app in the terminal is equivalent to python -c 'import uvicorn; uvicorn.run("test:app")'. This works fine.

Screencast.from.2024-04-25.20-51-18.webm

However, for some reason, python -c 'import uvicorn, test; uvicorn.run(test.app)' does not. The server starts correctly and the WS is also connected. But for some reason no change is ever pushed back to the client.

Screencast.from.2024-04-25.20-59-07.webm
@pmeier
Copy link
Collaborator Author

pmeier commented Apr 25, 2024

It seems to be some import side effect. Importing the test module before running the server somehow breaks the functionality: python -c 'import uvicorn, test; uvicorn.run("test:app")'

@pmeier
Copy link
Collaborator Author

pmeier commented Apr 25, 2024

It seems to be caused by the fact that we instantiate app in the test module. If we wrap into a make_app function, we can do python -c 'import uvicorn, test; uvicorn.run(test.make_app, factory=True)'. However, python -c 'import uvicorn, test; uvicorn.run(test.make_app())' still doesn't work.

My guess is that creating the app triggers some side effect that is only allowed to happen after the uvicorn server is started.

@pmeier
Copy link
Collaborator Author

pmeier commented Apr 25, 2024

Digging deeper, uvicorn.run internally uses asyncio.run. The app has to be created within this call rather than before to work correctly. The config is loaded at this point, which is why loading the app by string or creating it from the factory works.

@pmeier
Copy link
Collaborator Author

pmeier commented Apr 25, 2024

@philippjfr before I dig any deeper here, do you know of any side effects that the panel code could have that could cause a later execution of it by asyncio.run to somehow impede WS functionality?

@philippjfr
Copy link
Collaborator

before I dig any deeper here, do you know of any side effects that the panel code could have that could cause a later execution of it by asyncio.run to somehow impede WS functionality?

I do not but will dig in a little more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants