question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typer/Fastapi/asyncio interaction bug

See original GitHub issue

First 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:open
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
JarroVGITcommented, Sep 12, 2022

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 😃

0reactions
eprparadocscommented, Sep 12, 2022

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:

Okay, I’ll try it.

C.

On Mon, Sep 12, 2022 at 12:34 PM Jarro van Ginkel < @.***> wrote:

Not completely sure (and I am on my phone) but I thought you could just pass an ASGI object (like FastAPI) to Uvicorn, rather than the string with filename:app. It should work then.

— Reply to this email directly, view it on GitHub https://github.com/tiangolo/fastapi/issues/5383#issuecomment-1243995545, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAATQTLIN6J75V2X2FLYF5TV55LQTANCNFSM6AAAAAAQKUH6FE . You are receiving this because you authored the thread.Message ID: @.***>

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found