couchdb-python change notifications

て烟熏妆下的殇ゞ 提交于 2019-12-04 19:39:10

问题


I'm trying to use couchdb.py to create and update databases. I'd like to implement notification changes, preferably in continuous mode. Running the test code posted below, I don't see how the changes scheme works within python.

class SomeDocument(Document):

#############################################################################

#    def __init__ (self):

    intField  = IntegerField()#for now - this should to be an integer
    textField = TextField()

couch = couchdb.Server('http://127.0.0.1:5984')
databasename = 'testnotifications'

if databasename in couch:
    print 'Deleting then creating database ' + databasename + ' from server'
    del couch[databasename]
    db = couch.create(databasename)
else:
    print 'Creating database ' + databasename + ' on server'
    db = couch.create(databasename)

for iii in range(5):

    doc = SomeDocument(intField=iii,textField='somestring'+str(iii))
    doc.store(db)
    print doc.id + '\t' + doc.rev

something = db.changes(feed='continuous',since=4,heartbeat=1000)

for iii in range(5,10):

    doc = SomeDocument(intField=iii,textField='somestring'+str(iii))
    doc.store(db)
    time.sleep(1)
    print something
    print db.changes(since=iii-1)

The value

db.changes(since=iii-1) 

returns information that is of interest, but in a format from which I haven't worked out how to extract the sequence or revision numbers, or the document information:

{u'last_seq': 6, u'results': [{u'changes': [{u'rev': u'1-9c1e4df5ceacada059512a8180ead70e'}], u'id': u'7d0cb1ccbfd9675b4b6c1076f40049a8', u'seq': 5}, {u'changes': [{u'rev': u'1-bbe2953a5ef9835a0f8d548fa4c33b42'}], u'id': u'7d0cb1ccbfd9675b4b6c1076f400560d', u'seq': 6}]}

Meanwhile, the code I'm really interested in using:

db.changes(feed='continuous',since=4,heartbeat=1000)

Returns a generator object and doesn't appear to provide notifications as they come in, as the CouchDB guide suggests ....

Has anyone used changes in couchdb-python successfully?


回答1:


I use long polling rather than continous, and that works ok for me. In long polling mode db.changes blocks until at least one change has happened, and then returns all the changes in a generator object.

Here is the code I use to handle changes. settings.db is my CouchDB Database object.

since = 1
while True:
    changes = settings.db.changes(since=since)
    since = changes["last_seq"]
    for changeset in changes["results"]:
        try:
           doc = settings.db[changeset["id"]]
        except couchdb.http.ResourceNotFound:
           continue
        else:
           // process doc

As you can see it's an infinite loop where we call changes on each iteration. The call to changes returns a dictionary with two elements, the sequence number of the most recent update and the objects that were modified. I then loop through each result loading the appropriate object and processing it.

For a continuous feed, instead of the while True: line use for changes in settings.db.changes(feed="continuous", since=since).




回答2:


I setup a mailspooler using something similar to this. You'll need to also load couchdb.Session() I also use a filter for only receiving unsent emails to the spooler changes feed.

from couchdb import Server

    s = Server('http://localhost:5984/')
    db = s['testnotifications']
    # the since parameter defaults to 'last_seq' when using continuous feed
    ch = db.changes(feed='continuous',heartbeat='1000',include_docs=True)

    for line in ch:
        doc = line['doc']
        // process doc here
        doc['priority'] = 'high'
        doc['recipient'] = 'Joe User'
        # doc['state'] + 'sent'
        db.save(doc)

This will allow you access your doc directly from the changes feed, manipulate your data as you see fit, and finally update you document. I use a try/except block on the actual 'db.save(doc)' so I can catch when a document has been updated while I was editing and reload the doc before saving.



来源:https://stackoverflow.com/questions/7840383/couchdb-python-change-notifications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!