Skip to content

Background Task with websocket #289

@DrPyser

Description

@DrPyser

Description

Is it possible to use BackgroundTasks with websockets?

I tried with a simple case:

def test():
   print('test')


@router.post("/test")
async def send_notification(background_tasks: BackgroundTasks):
   background_tasks.add_task(test)
   return {"message": "Notification sent in the background"}


@router.websocket("/ws")
async def listen(websocket: WebSocket, background_tasks: BackgroundTasks):
   await websocket.accept()
   background_tasks.add_task(test)

The send_notification http endpoint example works, but the listen websocket endpoint doesn't.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions