【发布时间】:2014-10-14 17:21:32
【问题描述】:
如何在 Protorpc 中使用任务队列(推送队列)。
我有一个登录页面表单,它在发送时会执行多个操作:
- 将字段保存在 DataStore 中
- 向表单的发件人发送电子邮件
- 将字段发送到第三方应用程序(比如 CRM)
表单发送是在服务器端用protopc实现的。
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 来说并不方便。
【问题讨论】:
标签: python performance google-app-engine task-queue protorpc