Fastapi: How to run async process together with uvicorn in FAST API

Created on 17 Sep 2019  路  13Comments  路  Source: tiangolo/fastapi

I am trying to execute AsyncIOScheduler() along with UVICORN in FastAPI but only 1 task will be executing at a time.
Can you pls help me out how to test background process in UVICORN

question

Most helpful comment

I am new to asyncio and fastapi and was looking for an answer to a question similar to the OP here then found this bug via Google. Note the answer link by @euri10 gives a 404 now but I found https://fastapi.tiangolo.com/advanced/events/ which works for me so just adding a note here for the next traveller.

All 13 comments

can you share some snippet that fails, what did you try ?

Can you pls help me out how to test background process in UVICORN

I haven't used AsyncIOScheduler, but typically async background tasks execute in the same event loop, and not in a background process. So you'll get blocking behavior (i.e., at most one task executing at a time) if your background tasks are themselves blocking or otherwise don't involve async io.

It's not yet clear from your question whether this is related to your problem, but saying "background process" made me think this might be relevant.

Thanks @euri10 and @dmontagu!

@SumitMBSI were you able to solve your problem? Otherwise, can you please provide a minimal, self-contained, example showing your error?

Thanks @euri10 and @dmontagu!

@SumitMBSI were you able to solve your problem? Otherwise, can you please provide a minimal, self-contained, example showing your error?

I guess I have the same question, so I won't create new issue for that.

I have an app which has an API to resolve various currencies rates (eg USD/EUR) in realtime, several upstream rates providers and in-memory cache. So I'd like to start coroutine in background which updates rates with some intervals and "asyncio.sleeps" between that calls, the web API gets data from that shared singleton cache object and calcs stuff based on that.

Currently I do it in Sanic with their "before_server_start" handler and call create_task on current loop. Server is single-threaded, there is only one loop, so everything works as expected.

Now I'd like to move it to FastAPI, but I don't see the way of starting some simple background coroutine like the one I mentioned. What is important - it should run in 1 single process and loop in case of some database interactions or other background executions which may cause race conditions.

@dmantis you can call asyncio.create_task in a server startup event and have the task loop forever with asyncio.sleep just as you described.

I actually do this myself in my own projects.

Let me know if that鈥檚 not clear enough.

@dmontagu How does one register or receive the startup event?

a simple search on the docs : https://fastapi.tiangolo.com/tutorial/events/

Thanks @dmontagu and @euri10 for the help here!

@SumitMBSI I think the original issue was solved, right?

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

I am new to asyncio and fastapi and was looking for an answer to a question similar to the OP here then found this bug via Google. Note the answer link by @euri10 gives a 404 now but I found https://fastapi.tiangolo.com/advanced/events/ which works for me so just adding a note here for the next traveller.

https://fastapi.tiangolo.com/tutorial/events/ will help
`
@app.on_event('startup')
def initial_task():
cronTask.start()

if __name__ == '__main__':
logger.debug('main:app started')
uvicorn.run("main:app", host='localhost', port= 8000, loop='asyncio', debug=True)
`

@brianshen1990, as I said, that link you just quoted gives a 404 error. Use the link I give.

@DMantis you can call asyncio.create_task in a server startup event and have the task loop forever with asyncio.sleep just as you described.

I actually do this myself in my own projects.

Let me know if that鈥檚 not clear enough.

You are a life saver. I have been trying and searching everywhere for last 10 days for async real time database update and read operation with uvicorn but failed. After reading your reply and specifically searching for async task loop forever, I arrived at this reply.

https://stackoverflow.com/questions/54153332/schedule-asyncio-task-to-execute-every-x-seconds

btw, when using async we have to make sure all methods and sub-methods and await is used when returning data from them.

Was this page helpful?
0 / 5 - 0 ratings