python - Google App Engine Python - Protorpc && 任务队列

标签 python performance google-app-engine task-queue protorpc

如何将任务队列(推送队列)与 Protorpc 一起使用。

我有一个登陆页面表单,在发送时可以执行多个操作:

  • 将字段保存在数据存储中
  • 向表单发件人发送电子邮件
  • 将字段发送到第三方应用程序(例如 CRM)

表单发送是在服务器端用protorpc实现的。

class FormRequest(messages.Message)
  field1 = messages.StringField(1, required=True)
  field2 = messages.StringField(2, required=True)

...

class FormApi(remote.Service):
  @remote.method(TravelRequest, message_types.VoidMessage)
  def insert(self, request):
    # Save the form in the DataStore
    travel = FormModel(field1=request.field1, field2=request.field2)
    travel.put()

    # Send an email to the client
    ...

    # Send the data to a third party
    ...

    return message_types.VoidMessage()

此解决方案陷入困境,因为用户需要等待所有请求时间。 (在本例中只有 2-3 秒,但对于登陆页面表单来说已经很多了)

一个好的解决方案是使用任务队列来最小化用户需要等待的时间:

(作为示例)

class ...
  @remote ...
  def ...
    # Save the form in the DataStore
    taskqueue.add(url='/api/worker/save_to_db', params={'field1': request.field1, 'field2': request.field2})
    # Send an email to the client
    taskqueue.add(url='/api/worker/send_email', params={'field1': request.field1, 'field2': request.field2})
    # Send the data to a third party (CRM)
    taskqueue.add(url='/api/worker/send_to_crm', params={'field1': request.field1, 'field2': request.field2})

“问题”是 protorpc 仅获取 json 对象作为请求。 如何使用 TaskQueue(Push) 做到这一点?

TaskQueue 的默认行为是将参数作为 urlencoded 字符串发送,这对于 protorpc 来说并不方便。

最佳答案

让我们为任务队列定义一个 Worker 服务:

class WorkersApi(remote.Service):
  @remote.method(TravelRequest, message_types.VoidMessage)
  def save_to_db(self, request):
    # Instead of write each parameter, I am using this "cheat"
    params = {}
    for field in request.all_fields():
      params[field.name] = getattr(request, field.name)

    # Save data in the datastore
    form_model = FormModel(**params)
    form_model.put()

    return message_types.VoidMessage()

请注意,我使用的是相同的 message真实请求和任务队列请求的对象(不需要为每个请求创建不同的 message 对象是一个很大的优点) 问题是如何将taskqueue与这个protorpc函数一起使用。

正如我在问题中所说,任务队列的默认行为并不方便。

解决方案是将原始请求/消息(在我们的示例中为 FormRequest)对象转换回字符串,并将 header 设置到任务队列,负载为 application/json .

代码如下:

# This format string is take from the util file in the protorpc folder in Google App Engine source code
format_string = '%Y-%m-%dT%H:%M:%S.%f'

params = {}
for field in request.all_fields():
  value = getattr(request, field.name)
  if (isinstance(value, datetime.datetime)):
    value = value.strftime(format_string)
  params[field.name] = value

taskqueue.add(url='/api/workers.save_to_db', payload=json.dumps(params), headers={'content-type':'application/json'})

对“电子邮件”和“客户关系管理”执行相同的操作。

关于python - Google App Engine Python - Protorpc && 任务队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26366588/

相关文章:

c++ - Shedskin - 编译错误

performance - Julia 1.0 中的缓慢(重复)矩阵乘法

java - 在 Google AppEngine 中使用 app.yaml

google-app-engine - 是否有类似 Google Analytics 的解决方案来跟踪对 Web 服务的 REST 调用?

python - 创建具有未知数量属性的类实例

python - 使用其中一个数据帧作为键将 Python 中的数据帧组合到字典中

python - 导入错误: No module named

c - 给定数据类型的算术运算成本是否随操作数值而变化?

javascript - 从服务器端返回 Javascript 对象有什么好处?

javascript - Mobile Safari (iPhone) 对 Javascript GET 请求有 30 秒以上的延迟