Skip to content

Route medical

run(config, background_tasks) async

Initiates a medical analysis task asynchronously.

This function queues a medical analysis task using the provided configuration. The task is executed in the background, and the response includes the task ID.

Parameters:

Name Type Description Default
config AppConfig

Configuration object containing details such as task ID, user, and analysis type.

required
background_tasks BackgroundTasks

FastAPI's background task manager to handle asynchronous task execution.

required

Returns:

Name Type Description
JSONResponse

A JSON response containing the task ID, e.g., {"task_id": }.

Raises:

Type Description
HTTPException

If an error occurs while queuing the task, an HTTP 500 error is returned

Source code in view/api/route_medical.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@router_medical.post("/medical")
async def run(config: AppConfig, background_tasks: BackgroundTasks):
    """
    Initiates a medical analysis task asynchronously.

    This function queues a medical analysis task using the provided configuration. 
    The task is executed in the background, and the response includes the task ID.

    Args:
        config (AppConfig): Configuration object containing details such as task ID, user, 
            and analysis type.
        background_tasks (BackgroundTasks): FastAPI's background task manager to handle
            asynchronous task execution.

    Returns:
        JSONResponse: A JSON response containing the task ID, e.g., {"task_id": <task_id>}.

    Raises:
        HTTPException: If an error occurs while queuing the task, an HTTP 500 error is returned 
        with a relevant message.
    """
    try:
        background_tasks.add_task(medical.module_medical, config)
        logger.info(f"Task {config.task_id} has been queued successfully.")
        return JSONResponse({'task_id': config.task_id})
    except Exception as e:
        logger.error(f"Failed to queue task {config.task_id}: {e}")
        raise HTTPException(status_code=500, detail="An error occurred while queuing the task.")