未知

zc.catalogqueue的Python项目详细描述


目录队列提供目录索引的队列。基本思想 将目录操作排队,以便:

  • 操作可以成批进行,以提高效率
  • 应用程序请求不必等待索引编制

当文本索引时,排队的好处尤其显著 使用。

Detailed Documentation

Using Queues

通过实例化 zc.catalogqueue.queue.catalogqueue对象:

>>> import zc.catalogqueue.queue
>>> queue = zc.catalogqueue.queue.CatalogQueue()

我们可以传递队列大小。它应该是一个素数。默认值为 1009,有点大。

>>> queue = zc.catalogqueue.queue.CatalogQueue(11)

通常,队列注册为 zc.catalogqueue.interfaces.icatalogqueue实用程序。

>>> import zope.interface, pprint
>>> pprint.pprint(sorted(zope.interface.providedBy(queue)), width=1)
[<InterfaceClass zc.catalogqueue.interfaces.ICatalogQueue>,
 <InterfaceClass persistent.interfaces.IPersistent>]

有一些信息是队列自己维护的 正在处理状态。上次处理的时间和 已处理的编录事件可用。因为这个队列 经过处理后,它们有一些初始值:

>>> print queue.lastProcessedTime
None
>>> queue.totalProcessed
0

队列的长度提供对挂起的编目数的访问 事件:

>>> len(queue)
0

队列有两种使用方式。当内容被修改时,我们称之为add, 更新和删除队列上的方法:

>>> queue.add(1)
>>> queue.update(1)
>>> queue.remove(1)
>>> queue.update(2)
>>> queue.update(2)
>>> queue.add(3)
>>> queue.update(3)
>>> queue.add(3)
Traceback (most recent call last):
...
TypeError: Attempt to add an object that is already in the catalog
>>> queue.update(4)
>>> queue.update(4)
>>> queue.update(4)
>>> queue.remove(5)
>>> queue.update(5)
Traceback (most recent call last):
...
TypeError: Attempt to change an object that has been removed
>>> queue.update(0)
>>> queue.update(0)

此时,我们添加了几个事件,但尚未处理队列,因此 我们希望lastProcessedTimetotalProcessed保持不变,但是 反映挂起任务的队列长度:

>>> print queue.lastProcessedTime
None
>>> queue.totalProcessed
0
>>> len(queue)
6

周期性地,我们在队列上调用进程。我们需要通过身份证 对象和注入(目录)对象的集合:

>>> class Ids:
...     def queryObject(self, id, default=None):
...         if not id:
...             return default
...         return "object %s" % id
>>> class Injection:
...     def __init__(self, name):
...         self.name = name
...     def index_doc(self, docid, value):
...         print self.name, 'indexing', docid, value
...     def unindex_doc(self, docid):
...         print self.name, 'unindexing', docid
>>> queue.process(Ids(), [Injection('cat1'), Injection('cat2')], 10)
cat1 unindexing 1
cat2 unindexing 1
cat1 indexing 2 object 2
cat2 indexing 2 object 2
cat1 indexing 3 object 3
cat2 indexing 3 object 3
cat1 indexing 4 object 4
cat2 indexing 4 object 4
cat1 unindexing 5
cat2 unindexing 5
6

关于这个例子,有很多事情要注意:

  • 每个对象只处理一次。
  • 发生什么取决于最后一件事。
  • 对象0未被索引,因为QueryObject未返回任何值。我们 忽略已从IntID中删除的对象的事件 公用事业。
  • 返回处理的对象数。

队列上的处理信息已更新:

>>> queue.lastProcessedTime  # doctest: +ELLIPSIS
datetime.datetime(... tzinfo=<UTC>)
>>> queue.totalProcessed
6
>>> previous_time = queue.lastProcessedTime

队列的长度现在表示没有其他事件等待:

>>> len(queue)
0

如果我们在没有附加事件的情况下处理队列,只会得到0 后退:

>>> queue.process(Ids(), [Injection('cat1'), Injection('cat2')], 10)
0

更新历史处理信息:

>>> queue.lastProcessedTime  # doctest: +ELLIPSIS
datetime.datetime(... tzinfo=<UTC>)
>>> queue.lastProcessedTime > previous_time
True
>>> queue.totalProcessed
6
>>> len(queue)
0

当然,limit参数限制我们处理的事件数:

>>> for i in range(10):
...     queue.update(i)
>>> len(queue)
10
>>> queue.process(Ids(), [Injection('cat1')], 5)
cat1 indexing 1 object 1
cat1 indexing 2 object 2
cat1 indexing 3 object 3
cat1 indexing 4 object 4
5
>>> queue.totalProcessed
11
>>> len(queue)
5
>>> queue.process(Ids(), [Injection('cat1')], 5)
cat1 indexing 5 object 5
cat1 indexing 6 object 6
cat1 indexing 7 object 7
cat1 indexing 8 object 8
cat1 indexing 9 object 9
5
>>> queue.totalProcessed
16
>>> len(queue)
0

(请记住,0不会被处理,因为找不到它。)

当找不到对象时,将记录一条警告:

>>> import zope.testing.loggingsupport
>>> handler = zope.testing.loggingsupport.InstalledHandler('zc')
>>> queue.update(0)
>>> queue.process(Ids(), [Injection('cat1')], 5)
1
>>> print handler
zc.catalogqueue.queue WARNING
  Couldn't find object for 0
>>> handler.uninstall()

Edgecase

如果“旧”状态有两个“添加”事件,并且提交的状态处理 队列,并且“new”状态修改一个标记为要添加的对象,即 代码将另一个标记为要删除。

>>> from zc.catalogqueue.CatalogEventQueue import (
...     CatalogEventQueue, ADDED, REMOVED, CHANGED, CHANGED_ADDED)
>>> cq = CatalogEventQueue()
>>> def resolve(old, committed, new):
...     return sorted(cq._p_resolveConflict(
...        {'_conflict_policy': 0, '_data': old},
...        {'_conflict_policy': 0, '_data': committed},
...        {'_conflict_policy': 0, '_data': new}
...        )['_data'].items())
>>> resolve({1: (1, ADDED), 2: (1, ADDED)}, {},
...         {1: (1, ADDED), 2: (3, REMOVED), 3: (1, ADDED)})
[(2, (3, 0)), (3, (1, 1))]

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
在IE10中重新加载另一个帧时,internet explorer Java小程序失败/消失   ThreadLocal变量的java性能   java系统。出来println不是打印输出   java从JAXB类获取元素属性   java组织。天啊。科尔巴。包裹。InvalidName:IDL:omg。org/CORBA/ORB/InvalidName:1.0   java有没有办法让非事务连接抛出异常?   java是否有任何方法可以使用JdbcTemplate和查询/条件Fluent API   javajpa级联类型。刷新不工作?   未考虑java Maven依赖关系管理   java MySQL MBR包含抛出MySQLExceptionError的语句   java验证整数并将其设为5位数   java发现了循环依赖的问题   java Hibernate left join fetch到使用@ManyToMany关联映射的softdeleted实体生成无效查询?   JavaH:commandButton多个操作:下载文件并呈现ajax表   Google Contacts API在Java、C#、Python或Ruby中是否有一个Hello World示例?