couchdb-python 更改通知
我正在尝试使用 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)
返回生成器对象,并且似乎没有在通知进来时提供通知,如 CouchDB guide< /a> 建议....
有没有人使用过更改couchdb-python 成功了吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用长轮询而不是连续轮询,这对我来说效果很好。在长轮询模式下,
db.changes
会阻塞,直到至少发生一项更改,然后返回生成器对象中的所有更改。这是我用来处理更改的代码。
settings.db
是我的 CouchDB 数据库对象。正如您所看到的,这是一个无限循环,我们在每次迭代中调用
changes
。对changes
的调用返回一个包含两个元素的字典,即最近更新的序列号和修改的对象。然后,我循环遍历每个结果,加载适当的对象并对其进行处理。对于连续提要,请使用
for settings.db.changes(feed="continuous",since=since)
中的更改,而不是使用while True:
行。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.As you can see it's an infinite loop where we call
changes
on each iteration. The call tochanges
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 usefor changes in settings.db.changes(feed="continuous", since=since)
.我使用类似的东西设置了一个邮件处理程序。您还需要加载 couchdb.Session() 我还使用一个过滤器来仅接收未发送到后台处理程序更改源的电子邮件。
这将允许您直接从更改源访问您的文档,按照您认为合适的方式操作您的数据,最后更新您的文档。我在实际的“db.save(doc)”上使用了 try/ except 块,这样我就可以在编辑时捕获文档的更新情况,并在保存之前重新加载文档。
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.
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.