Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何实现支持名称空间的FIFO队列_Python_Google App Engine_Queue_Fifo - Fatal编程技术网

Python 如何实现支持名称空间的FIFO队列

Python 如何实现支持名称空间的FIFO队列,python,google-app-engine,queue,fifo,Python,Google App Engine,Queue,Fifo,我使用以下方法来处理基于GoogleAppEngine db.Model()的FIFO队列 此队列按预期工作(非常好) 现在,我的代码有一个方法可以访问延迟队列调用的FIFO队列: def deferred_worker(): data= QueueItem.pop() do_something_with(data) 我想增强这个方法和队列数据结构,添加一个client_ID参数,表示需要访问自己队列的特定客户机。 比如: def deferred_worker

我使用以下方法来处理基于GoogleAppEngine db.Model()的FIFO队列

此队列按预期工作(非常好)

现在,我的代码有一个方法可以访问延迟队列调用的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)
如何将队列编码为可识别客户端ID

约束条件:
-客户端的数量是动态的,不是预定义的
-Taskqueue不是一个选项(1.最多10个队列2.我想完全控制我的队列)

您知道如何使用新的(请记住,我不是从webapp.RequestHandler调用db.Model)添加此行为吗?
另一个选项:我可以向QueueItem添加一个
客户端ID db.StringProperty
,使用它有一个pull上的过滤器方法:

QueueItem.all(keys_only=True).filter(client_ID=an_ID).order('created').fetch(10)
有更好的主意吗?

假设您的“客户机类”实际上是客户机调用的请求处理程序,您可以这样做:

from google.appengine.api import users
from google.appengine.api.namespace_manager import set_namespace

class ClientClass(webapp.RequestHandler):
  def get(self):
    # For this example let's assume the user_id is your unique id.
    # You could just as easily use a parameter you are passed.
    user = users.get_current_user()
    if user:
       # If there is a user, use their queue.  Otherwise the global queue.
       set_namespace(user.user_id())

    item = QueueItem.pop()
    self.response.out.write(str(item))

    QueueItem.push('The next task.')
或者,也可以设置名称空间


通过设置默认名称空间,对数据存储的所有调用都将“在”该名称空间内,除非您明确指定其他名称空间。请注意,要获取和运行任务,您必须知道名称空间。因此,您可能希望在默认名称空间中维护一个名称空间列表,以便进行清理。

正如我在回答您对我的原始答案的查询时所说,您不需要做任何事情就可以使用名称空间:构建队列的数据存储已经支持名称空间。只需根据需要设置名称空间,如中所述。

在您的应用程序中,客户端会话是匿名的或经过身份验证的?@Paulo它是一个类客户端;我没有用户。我认为名称空间api以多租户问题域为目标,因此如果您可以将类行为映射到此问题域,那么这是可能的。@Robert我不需要在QueueItem.pop()语句之后将\u名称空间(默认\u名称空间)设置为默认吗?执行其他db操作的并发任务不可能使用错误的命名空间吗?调用set_namespace(user.user_id())时,它是为所有应用程序全局设置的还是仅为webapp.RequestHandler线程设置的?否。如果按照建议设置名称空间,set_namespace将仅应用于该请求。获取名称空间的调用是由API的底层代码根据需要进行的。@Robert如果您查看名称空间API页面,下面是一个示例,其中包含一个最终还原保存的名称空间的示例。为什么?我想你说的是“在数据存储中”的例子。他们正在另一个命名空间中运行请求。它们在当前名称空间和“-global-”名称空间内更新计数器。他们希望确保,如果更新“-global-”计数器的调用失败,他们将返回到“正确”的命名空间,否则任何进一步的API调用都将在“-global-”命名空间内。您推迟了某项任务,对吗?延迟使用后端上的任务队列。任务队列应该保留名称空间。对不起,我不明白。当我调用set_namespace(..)时,它是否为所有应用程序全局设置?此调用的作用域是否会引发并发问题为其他并发调用设置错误的命名空间?它是为当前请求全局设置的。在任何情况下,Python运行时都是单线程的,因此并发请求不会出现问题。
from google.appengine.api import users
from google.appengine.api.namespace_manager import set_namespace

class ClientClass(webapp.RequestHandler):
  def get(self):
    # For this example let's assume the user_id is your unique id.
    # You could just as easily use a parameter you are passed.
    user = users.get_current_user()
    if user:
       # If there is a user, use their queue.  Otherwise the global queue.
       set_namespace(user.user_id())

    item = QueueItem.pop()
    self.response.out.write(str(item))

    QueueItem.push('The next task.')