Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
Use FastAPI to interact with async loop

I am running coroutines of 'workers' whose job it is to wait 5s, get values from an asyncio.Queue() and print them out continually.

q = asyncio.Queue()

def worker():
    while True:
        await asyncio.sleep(5)
        i = await q.get()
        print(i)
        q.task_done()

async def main(q):
    workers = [asyncio.create_task(worker()) for n in range(10)]
    await asyncio.gather(*workers)


if __name__ == "__main__":
    asyncio.run(main())

I would like to be able to interact with the queue through http requests using FastAPI. For example POST requests that would 'put' items in the queue for the workers to print.

I'm unsure how I can run the coroutines of the workers concurrently with FastAPI to achieve this effect. Uvicorn has its own event loop I believe and my attempts to use asyncio methods have been unsuccessful.

The router would look something like this I think.

@app.post("/")
async def put_queue(data:str):
    return q.put(data)

And I'm hoping there's something that would have an effect like this:

await asyncio.gather(main(),{FastApi() app run})
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

One option would be to add a task that wraps your main coroutine in a on startup event

import asyncio
@app.on_event("startup")
async def startup_event():
    asyncio.create_task(main())

This would schedule your main coroutine before the app has been fully started.

Important here is that you don't await the created task as it would basically block startup_event forever

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!