Typer/Fastapi/asyncio interaction bug
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
import os
import asyncio
from fastapi import FastAPI
import uvicorn
app = FastAPI()
@app.on_event("startup")
async def on_startup() -> None:
# WHEN YOU GET HERE YOU WILL FIND THE state.invisible IS GONE!!!!
foo = 2
@app.on_event("shutdown")
async def on_shutdown() -> None:
# When the system is shutdown
pass
async def serverapp(appname:str, serverip:str, ctrlport:int):
config = uvicorn.Config(appname, host=serverip, port=ctrlport)
server = uvicorn.Server(config)
# state.invisible IS STILL PRESENT!!!!
await server.serve()
if __name__ == "__main__":
import asyncio
import typer
import sys
import os
def main(serverip:str = typer.Argument(..., help="IP address of server", metavar='IPaddress'),
ctrlport:int = typer.Argument(..., help="PORT of control message server", metavar='int')):
# Create the special name passed into uvicorn!
mainName = os.path.basename(sys.argv[0]).split('.')[0] + ':app'
# Spin up the server!
app.state.invisible = 'This will be gone when you get to on_start!'
asyncio.run(serverapp(mainName, serverip, ctrlport ))
typer.run(main)
Description
If you run the code you will find that app.state is no longer available in on_start() though it was set in main().
Set a breakpoint at foo = 2 in on_start() and examine app.start - you wlll see it has disappeared.
It turns out, stepping through the code you see the original module containing ‘app’ is reloaded!!!
Operating System
Linux
Operating System Details
Linux slickus 5.15.0-48-generic #54-Ubuntu SMP Fri Aug 26 13:26:29 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
FastAPI Version
0.75.0
Python Version
3.10.6
Additional Context
None
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
The Ultimate FastAPI Tutorial Part 9 - Asynchronous ...
In part 9 of the FastAPI tutorial, we'll look at utilizing async IO to ... By leveraging Python's new Asynchronous IO (async IO)...
Read more >fastapi.exceptions.FastAPIError: Invalid args for response field ...
The error says your type you pass to predict must be pydantic BaseModel (or dataclass from 0.67.0 version of FastApi).
Read more >Concurrency and async / await - FastAPI
Write your own async code Starlette (and FastAPI) are based on AnyIO, which makes it compatible with both Python's standard library asyncio and...
Read more >We went all in on FastAPI with my team, but we're hit the issue ...
We've already been hit by multiple bugs with fixing PR opened, ... How is Django this day with modern Python constructs like typing...
Read more >Developing and Testing an Asynchronous API with FastAPI ...
Test a FastAPI app with pytest; Interact with a Postgres database asynchronously; Containerize FastAPI and Postgres inside a Docker container ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Screaming (using multiple exclamation marks) and things like “Excuse me?” really doesn’t work in your favour when coming to a public place to get help for free from volunteers. Just saying, do with it what you will 😃
That did it! I followed the examples too carefully without reading the API docs. Thanks.
C.
On Mon, Sep 12, 2022 at 12:40 PM Charles Wegrzyn @.***> wrote: