anna-money / asyncpg-listen Goto Github PK
View Code? Open in Web Editor NEWA thing to simplify listening for PG notifications with asyncpg
License: MIT License
A thing to simplify listening for PG notifications with asyncpg
License: MIT License
When I (pip) install asyncpg-listen and try to run the example code in the README.md, I get error "AttributeError: __aenter__" at line "async with asyncpg.connect() as connection:".
I'm running the latest asyncpg, 0.25.0, which doesn't support connections as context managers:
MagicStack/asyncpg#583
so I am not sure how you got the example to work as written.
Many thanks for sharing and maintaining this valuable project. Is there a way to use the connect_func with a connection from a connection pool?
pgpool = await asyncpg.create_pool(dsn=myl_dsn)
async with pgpool.acquire() as connection:
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 ?")
`
This library requires Python 3.9 or greater, yet asyncpg itself has a supports 3.6 and later.
What is the technical requirement driving this decision (it's not obvious at first glance)?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.