我正在使用以下方法来处理基于 Google App Engine db.Model 的 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?
约束:
- 客户端数量是动态的,不是预定义
的 - 任务队列不是一个选项(1. 最多十个队列 2. 我想完全控制我的队列)
您知道如何使用新的Namespaces api添加此行为(请记住,我不是从 webapp.RequestHandler 调用 db.Model)?
另一种选择:我可以添加一个client_ID db.StringProperty
到 QueueItem 使用它有一个拉方法过滤器:
QueueItem.all(keys_only=True).filter(client_ID=an_ID).order('created').fetch(10)
有更好的主意吗?