Sri
Sri10mo ago

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:
@on_change_query("file:rpc")
def on_change(query_result: QueryResult):
# handle logic
@on_change_query("file:rpc")
def on_change(query_result: QueryResult):
# handle logic
@on_change_table("table...")
def on_change(record: RecordChangeEvent):
# handle logic
@on_change_table("table...")
def on_change(record: RecordChangeEvent):
# handle logic
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
Convex Bot
Convex Bot10mo ago
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!
dowski
dowski10mo ago
check out the usage example here which makes use of the subscribe method: https://github.com/get-convex/convex-py?tab=readme-ov-file#convex
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.
ballingt
ballingt10mo ago
@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.
Sri
SriOP10mo ago
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!
ballingt
ballingt10mo ago
@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
Sri
SriOP10mo ago
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.
ballingt
ballingt10mo ago
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
Sri
SriOP10mo ago
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
ballingt
ballingt10mo ago
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'"
Sri
SriOP10mo ago
cool this is pretty slick... to scale out large jobs that require gpus/multiple cores... with very little code and logic!

Did you find this page helpful?