Unable to reload inside an event loop using 'create_task' #1843
-
Kindly analyze the following code snippet and clarify if this is a potential problem or the code has some flaws, currently it is not possible to load inside a running event loop # built-in
import asyncio
# external
import uvicorn
from fastapi import FastAPI
def main():
event_loop = asyncio.new_event_loop()
app_fastapi = FastAPI()
server = uvicorn.Server(
config=uvicorn.Config(
app_fastapi, # also passing the module as main:app_fastapi and moving this to global does not work
reload=True,
loop="uvloop",
),
)
event_loop.create_task(server.serve())
event_loop.run_forever()
if __name__ == "__main__":
main() The following is another snipper of code that behaves the same way as above # built-in
import asyncio
# external
import uvicorn
from fastapi import FastAPI
async def main():
app_fastapi = FastAPI()
server = uvicorn.Server(
config=uvicorn.Config(
app_fastapi, # also passing the module as main:app_fastapi and moving this to global does not work
reload=True,
loop="uvloop",
),
)
api = asyncio.create_task(server.serve())
await asyncio.wait([api])
if __name__ == "__main__":
asyncio.run(main()) Once running, reload will not work upon file changes. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
That's correct. The Lines 453 to 574 in 23b9f05 |
Beta Was this translation helpful? Give feedback.
-
Is there a workaround other than implement my own reloader? |
Beta Was this translation helpful? Give feedback.
That's correct. The
reload
only works when usinguvicorn.run
.uvicorn/uvicorn/main.py
Lines 453 to 574 in 23b9f05