我正在使用以下方法来处理基于Google App Engine db.Model的FIFO队列(see this question)。如何实现支持名称空间的FIFO队列
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import run_wsgi_app
class QueueItem(db.Model):
created = db.DateTimeProperty(required=True, auto_now_add=True)
data = db.BlobProperty(required=True)
@staticmethod
def push(data):
"""Add a new queue item."""
return QueueItem(data=data).put()
@staticmethod
def pop():
"""Pop the oldest item off the queue."""
def _tx_pop(candidate_key):
# Try and grab the candidate key for ourselves. This will fail if
# another task beat us to it.
task = QueueItem.get(candidate_key)
if task:
task.delete()
return task
# Grab some tasks and try getting them until we find one that hasn't been
# taken by someone else ahead of us
while True:
candidate_keys = QueueItem.all(keys_only=True).order('created').fetch(10)
if not candidate_keys:
# No tasks in queue
return None
for candidate_key in candidate_keys:
task = db.run_in_transaction(_tx_pop, candidate_key)
if task:
return task
此队列按预期工作(非常好)。
现在我的代码有访问由延期队列调用这个FIFO队列的方法:
def deferred_worker():
data= QueueItem.pop()
do_something_with(data)
我想加强这方面的方法和队列数据结构将代表一个特定的客户端的client_ID参数需要访问自己的队列。 喜欢的东西:
def deferred_worker(client_ID):
data= QueueItem_of_this_client_ID.pop() # I need to implement this
do_something_with(data)
我怎么能编写的队列中CLIENT_ID知道?
约束:
- 客户端的数量是动态的,而不是预定义
- TASKQUEUE不是一个选项(1. 10最大队列2.我想对我的队列完全控制)
待办事项你知道我怎么可以使用新的Namespaces api添加这个行为(记住,我没有从webapp.RequestHandler调用db.Model)?
另一种选择:
QueueItem.all(keys_only=True).filter(client_ID=an_ID).order('created').fetch(10)
任何更好的想法:我可以用它在拉法的过滤器增加一个client_ID db.StringProperty
到QueueItem?
在您的应用中,客户端会话是匿名还是身份验证? – 2010-10-18 21:50:35
@Paulo它是一个类客户端;我没有用户。 – systempuntoout 2010-10-18 21:52:37
我认为命名空间API针对多租户问题域,所以如果你凸轮映射你的类行为到这个问题域,那么这是可能的。 – 2010-10-18 22:57:28