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
将远程FIFO队列作为Python GAE应用程序实现的最简单方法是什么?_Python_Google App Engine - Fatal编程技术网

将远程FIFO队列作为Python GAE应用程序实现的最简单方法是什么?

将远程FIFO队列作为Python GAE应用程序实现的最简单方法是什么?,python,google-app-engine,Python,Google App Engine,将远程FIFO队列实现为Python GAE应用程序,然后向其推/拉名称-值对字典的最简单方法是什么 例如,当对GAE应用程序执行http get时,GAE应用程序将返回发布到该应用程序的最早的名称-值对集合,这些集合以前未从队列中提取。然后,这些名称-值对将在客户端重新实例化为字典。urllib.urlencode提供了一种将字典编码为参数的简单机制,但是当您http“获取”参数时,将参数解码为字典的类似简单方法是什么?当队列中没有项目时,GAE应用程序应返回null或客户端可以响应的其他更合

将远程FIFO队列实现为Python GAE应用程序,然后向其推/拉名称-值对字典的最简单方法是什么

例如,当对GAE应用程序执行http get时,GAE应用程序将返回发布到该应用程序的最早的名称-值对集合,这些集合以前未从队列中提取。然后,这些名称-值对将在客户端重新实例化为字典。urllib.urlencode提供了一种将字典编码为参数的简单机制,但是当您http“获取”参数时,将参数解码为字典的类似简单方法是什么?当队列中没有项目时,GAE应用程序应返回null或客户端可以响应的其他更合适的标识符

#A local python script
import urllib 
targetURL="http://myapp.appspot.com/queue"

#Push to dictionary to GAE queue
params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
f = urllib.urlopen(targetURL, params)
print f.read()
params = urllib.urlencode({'foo': 1, 'bar': 2})
f = urllib.urlopen(targetURL, params)
print f.read()


#Pull oldest set of name-value pairs from the GAE queue and create a local dictionary from them.
#f = urllib.urlopen(targetURL, ……)
#returnedDictionary = ????
实现这个短GAE应用程序的最简单方法是什么

#queue.py a url handler in a GAE application.  
# For posts, create an object from the posted name-value pairs and insert it into the queue as the newest item in the queue
# For gets, return the name-value pairs for the oldest object in the queue and remove the object from the queue.
#   If there are no items in the queue, return null

下面假设您正在使用webapp框架

简单的答案是,您只需使用self.request.GET,它是一个包含发送到请求的表单数据的MultiDict(在许多情况下可以将其视为dict)


注意,HTTP允许表单数据多次包含相同的键;如果您想要的不是dict,而是已发送到应用程序的键值对列表,那么您可以使用self.request.get.items()获得这样的列表(请参阅),然后将这些对添加到队列中。

大致如下:

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

class QueueHandler(webapp.RequestHandler):
  def get(self):
    """Pop a request off the queue and return it."""
    self.response.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    task = QueueItem.pop()
    if not task:
      self.error(404)
    else:
      self.response.out.write(task.data)

  def post(self):
    """Add a request to the queue."""
    QueueItem.push(self.request.body)

一个警告:因为队列排序依赖于时间戳,所以在不同的机器上非常接近的任务可能会无序排队,因为没有全局时钟(只有NFS同步的服务器)。可能不是真正的问题,但这取决于您的用例。

您认为可以将名称空间支持添加到此队列吗?我想对不同的名称空间使用相同的队列。数据存储自动支持名称空间-只需设置名称空间并转到即可。谢谢,没有收到任何关于您的评论的红包通知,因此,正如您所知:-/,我就这个主题问了一个问题。