Closed FilterBody closed 9 months ago
Hi @FilterBody
Thank you for reporting this with reproduction steps.
I didn´t notice this because in such cases I was running with the uvicorn
CLI: the following works.
uvicorn main:app --reload --port 8887
The project templates I worked on work, too, because they don't try to configure routes in the same file used to run uvicorn programmatically.
I will take a look.
Ok this has to do with the new singleton router that was added as default one for the reasons explained here:
In this case, the singleton router is initialized before uvicorn
forks the process for its dynamic import, thus causing the issue. The application code gets executed more than once in the context of the same router (the router was already instantiated).
It is funny I didn't notice because the same does not happen when you run uvicorn
from the command line, or when you don´t import blacksheep in the files used to run uvicorn programmatically (like in my templates, as I wrote earlier - see this example)
The following works - just for the sake of example:
from datetime import datetime
from blacksheep import Application
from blacksheep.server.routing import Router
router = Router()
get = router.get
app = Application(router=router)
@get("/")
def home():
return f"Hello, World! {datetime.now().isoformat()}"
if __name__ == "__main__":
import uvicorn
uvicorn.run(app="main:app", port=8887, reload=True)
Okay, thank you very much
Let's keep this open, I want to fix this issue.
result
env