How to process REST requests asynchronously? Python, FastApi, SQLALchemy

Viewed 55

How to run POST requests (one after another) that calculate something without waiting for the end of calculations?

I want my FastApi work asynchronously. POST method accept two parameters: base and exponent and starts to calculate. The calculate method has a time.sleep() inside which depends on the exponent. The parameters (and the id, status and result) are collected in the database (SQLAlchemy). Status and Result should be updating during the calculation running (one can check the progress with GET request).

I want to process some POST/GET asynchronously, without waiting until the end of the calculation the request triggers.

Does anyone have an idea or hint how to make it work in Python? Thank you in advance.

@router.post("/tasks")
async def create_task(base: int, exponent: int, task_dal: TaskDAL = Depends(get_task_dal)):
    async with async_session() as session:
        async with session.begin():
            task_dal = TaskDAL(session)
            return await task_dal.create_task(base, exponent)


async def calculate(task: Task):
    current_result = 1
    for i in range(task.exponent):
        await time.sleep(1)
        current_result = task.base * current_result
        task.status = ((i / task.exponent) * 100)
    task.result = current_result

async def create_task(self, base: int, exponent: int):
    new_task = Task(base=base, exponent=exponent)
    asyncio.gather(TaskDAL.calculate(new_task))
    self.db_session.add(new_task)
    self.db_session.flush()

In case - all files are in my GitHub repository: https://github.com/wmaterkowska/asynchronous_task_python

0 Answers
Related