Python Based Real-time connection
Hey any plans to make the https://github.com/get-convex/convex-py client library reactive. Something like a websocket handler like:
I have some backend services that need python and would love it if there was a way for me to handle side effects in my python code. I know the workaround with actions, etc and pushing the behavior to my app but i would love it if the python portion of my code was also reactive! Thanks!
GitHub
GitHub - get-convex/convex-py: Python Client Library for Convex
Python Client Library for Convex. Contribute to get-convex/convex-py development by creating an account on GitHub.
11 Replies
Thanks for posting in <#1088161997662724167>.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.
- Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
- Use search.convex.dev to search Docs, Stack, and Discord all at once.
- Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI.
- Avoid tagging staff unless specifically instructed.
Thank you!
check out the usage example here which makes use of the
subscribe
method:
https://github.com/get-convex/convex-py?tab=readme-ov-file#convexGitHub
GitHub - get-convex/convex-py: Python Client Library for Convex
Python Client Library for Convex. Contribute to get-convex/convex-py development by creating an account on GitHub.
@Sri you could totally write a decorator that does this! You'd be subscribing to a Convex query function, not a table change—but that Convex query function can read whatever tables you want.
oh wow blind of me 😦 i missed the subscribe option 😂 i saw the rest of the mutations and didnt realize there is a subscribe this is amazing!
@Sri are you using async Python? There's a PR for async iteration and I've thought about this, but right now whether you want to use threads or asyncio is up to you
prefer asyncio i am currently running backend on modal functions. and i would like to dispatch containers for longer durable python code execution when a new entry happens.
I've found it easiest to run a thread that blocks on the subscription updates and use threadsafe queues to get communicate back to the rest of process
Cool, shouldn't be hard to hook up with the usual asyncio adapters for blocking calls but I'd be interested in making this more convenient for cases like yours
but standard loops also work. just curious based on the example when subscribing to a list query are the list of messages deduped or is the list query republish all the results to the python client
Each time you get the entire new query result (so in that example, list of messages) so if you want to use it like a work queue you should write a Convex query function that returns all "unclaimed" tasks and have yorurPython server "claim" the task by writing to the DB
subscriptions in Convex are always on the result of a query funciton, so the naive way to get changes is to diff these results on the client (but that doesnt' scale well)
so you probably want to write a function that returns whichever entities you're intrested in, e.g. "job.status === 'unclaimed'"
cool this is pretty slick... to scale out large jobs that require gpus/multiple cores... with very little code and logic!
Ian wrote about this a bit here https://stack.convex.dev/work-stealing https://stack.convex.dev/implementing-work-stealing