couchdb python更改通知

couchdb python更改通知,python,couchdb,couchdb-python,Python,Couchdb,Couchdb Python,我正在尝试使用couchdb.py创建和更新数据库。我希望实现通知更改,最好是在连续模式下。运行下面发布的测试代码,我看不出更改方案在python中是如何工作的 class SomeDocument(Document): ############################################################################# # def __init__ (self): intField = IntegerField()#f

我正在尝试使用couchdb.py创建和更新数据库。我希望实现通知更改,最好是在连续模式下。运行下面发布的测试代码,我看不出更改方案在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)
价值

db.changes(since=iii-1) 
返回感兴趣的信息,但格式尚未确定如何提取序列号、修订号或文档信息:

{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}]}
同时,我真正感兴趣的代码是:

db.changes(feed='continuous',since=4,heartbeat=1000)
返回一个generator对象,并且不会像CouchDB所建议的那样,在收到通知时提供通知


有人成功地使用了couchdb python中的更改吗?

我使用长轮询而不是连续轮询,这对我来说还可以。在长轮询模式下,
db.changes
阻塞,直到至少发生一次更改,然后返回生成器对象中的所有更改

下面是我用来处理更改的代码
settings.db
是我的CouchDB数据库对象

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
正如您所看到的,这是一个无限循环,我们在每次迭代中调用
更改
。调用
changes
返回一个包含两个元素的字典,即最近更新的序列号和修改的对象。然后,我在每个结果中循环加载适当的对象并对其进行处理


对于连续馈送,而不是
而为True:
行使用
来更改设置.db.changes(feed=“continuous”,since=since)
我使用类似的方法设置邮件后台处理程序。您还需要加载couchdb.Session(),我还使用了一个过滤器,用于仅接收发送到后台处理程序更改提要的未发送电子邮件

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)

这将允许您直接从changes提要访问您的文档,在您认为合适的时候操作您的数据,并最终更新您的文档。我在实际的“db.save(doc)”上使用了try/except块,这样我就可以在编辑文档时捕捉到文档的更新,并在保存之前重新加载文档。

关于这些问题,当与远程服务器(例如Cloudant)一起使用时,“自”oparg会引发错误。。。。编辑-这似乎是因为cloudant不将修订存储为整数,而将其存储为字符串,这使得自变量更加混乱!