Reputation: 780
I am using Cloudant for storing json-objects. They have an API for getting a continous feed with changes happening in the database. What I want to do is create a persistent connection towards this endpoint and call a function (callback) every time this response object gets updated. Cloudant has a built in function for this in Node-js, but nothing that can work in the background for python (using v 2.7).
How can I solve this? Haven't found anything useful online. The built-in functions in the Cloudant package doesn't work in the Flask API application that I have (they are just using never ending loops for retrieving the changes).
My idea is for the code to look something like this:
def callBack(event):
print(event)
session(url="username:password.cloudant.com/test/_changes?feed=continous", callback=callBack, feed="live")
Upvotes: 1
Views: 53
Reputation: 3737
You need an event loop (node has this built in, Python doesn't). I'd look at something like the gevent library.
There is a tutorial showing the use of long-poll with gevent here: http://sdiehl.github.io/gevent-tutorial/#long-polling that should be extendable to use the a streaming changes feed, like in https://github.com/xpqz/pylon/blob/master/pylon.py#L165.
Upvotes: 1