cloudant-labs / cloudant-python

Asynchronous Cloudant / CouchDB interface for Python
http://cloudant-labs.github.io/cloudant-python/
37 stars 17 forks source link

Make continuous changes feed more 'pythonic'. #31

Closed mgmarino closed 10 years ago

mgmarino commented 10 years ago

I apologize is this has been covered elsewhere, I didn't see it in any of the older issues.

Currently, if one requests a continuous feed, the returned object is a requests.Response object, where the proper way to iterate is:

acct = cloudant.Account(uri="http://127.0.0.1:5984")
db = acct["test"]
change = db.changes(params=dict(feed='continuous',heartbeat=1000))
for l in change.iter_lines():
    print l

The 'problem' with this is that the requests.Response.iter_lines function automatically buffers with chunk_size=512 (hard-coded) meaning that it will stall until this buffer length is reached. Generally a user would expect each line and heartbeat to return as soon as received. The expected behavior can be recovered by passing chunk_size=1 to the iter_lines function.

I think at the very least this should be clearly documented.

Ideally, when the feed is continuous, I would suggest returning the generator with the chunk_size correctly set instead of the response object (or perhaps a tuple so that both the generator and the object are available?) so that the user can do:

change = db.changes(params=dict(feed='continuous',heartbeat=1000))
for line in change:
     print line

which I think is somewhat cleaner. I'd be happy to put together a pull request, but I thought I'd get some comments first.

garbados commented 10 years ago

Fair points! I'll adapt your feedback into the readme and the changes iterator, and cut a new release. Thanks!

kxepal commented 10 years ago

See PR #33 , it's fixed there

garbados commented 10 years ago

Wahoo! Thanks again! Closing this so we can use PR #33 as the One True Discussion.