Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I pass additional args to the callback func #175

Open
mikcatta opened this issue Mar 17, 2023 · 2 comments
Open

How do I pass additional args to the callback func #175

mikcatta opened this issue Mar 17, 2023 · 2 comments

Comments

@mikcatta
Copy link

mikcatta commented Mar 17, 2023

I am trying to implement this in a FastAPI/Websockets app, so that when a table update has been made, the changes (or a simple message) is pushed back to the listening client. Each client could potentially be listening to different channels, in this example they are all listening to "customer2" .

What I need is to pass the websocket argument to the notification handler, so that instead of just printing it, it sends back the payload to whichever client was listening. This is where I am a bit stuck, as my knowledge of asyncio is limited. Any help most appreciated.

`
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()

try:
    listener = asyncpg_listen.NotificationListener(asyncpg_listen.connect_func(dsn))
    listener_task = asyncio.create_task(
      listener.run(
        {"customer2": handle_notifications},
        policy=asyncpg_listen.ListenPolicy.LAST,
        notification_timeout=50
        )
    )
    # add it to the list so they can be closed if the server is shutdown
    all_listener_tasks.append(listener_task)

except WebSocketDisconnect:
    logger.info('Client has disconnected') # maybe more needs to be done to clean up ....
    return

async def handle_notifications(notification: asyncpg_listen.NotificationOrTimeout) -> None:
print(f"{notification} has been received, sent to websocket ?")
`

@bitnom
Copy link

bitnom commented May 1, 2023

I'm about to need to do this in Blacksheep. Will report back what happens.

@baklim
Copy link

baklim commented Jul 2, 2024

You can use functools.partial.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants