简体   繁体   English

Google App Engine上的异步POST python请求

[英]Async POST python request on Google App Engine

I'm working on a python project on Google App Engine and I'm using Parse to send push notifications. 我正在Google App Engine上的python项目上工作,并且正在使用Parse发送推送通知。 Eyerything is working fine but I noticed some lag when I have to send multiple notifications to different devices given by POST rest request to Parse server. Eyerything正常运行,但是当我必须将多个通知发送到由POST休息请求给Parse服务器给出的不同设备时,我注意到了一些滞后。

To be clearer: 要更清楚:

I have a loop with N elements, for every element I have to send a push notifications and so I have to make a POST request to Parse and every connection (of course) take about 300msec to be completed, so it is quite slow to complete entire loop. 我有一个包含N个元素的循环,对于每个元素我都必须发送一个推送通知,因此我必须向Parse发出POST请求,并且每个连接(当然)大约需要300毫秒才能完成,因此完成它的速度相当慢整个循环。

I think should be better to change the request in async request but after searching on documentation and on Google I found no clear example how doing it with urllib2 or urlfetch and how pass headers with Parse Key and Applicaton Id.. 我认为在异步请求中更改请求应该更好,但是在文档和Google上搜索之后,我没有找到明确的示例如何使用urllib2urlfetch以及如何使用解析键和Applicaton ID传递标头。

Working (not async) code is 工作(非异步)代码是

    parse_connection = httplib.HTTPSConnection('api.parse.com', 443)
    parse_connection.connect()
    parse_app_id = parse_settings.APPLICATION_ID
    parse_rest_api_key = parse_settings.REST_API_KEY
    parse_connection.request('POST', '/1/push',
                            json.dumps({
                                "channels": ["blabla"],
                                "data": {
                                    "alert": "A",
                                    "title":"B",
                                    "badge": "Increment",
                                    "category": "C",
                                    "sound": "default",
                                    "additionalInfos": {"X": "Custom dict"}}
                            }),
                            {
                                "X-Parse-Application-Id": parse_app_id,
                                "X-Parse-REST-API-Key": parse_rest_api_key,
                                "Content-Type": "application/json"
                            })
    return json.loads(parse_connection.getresponse().read())

To make it async I think I should use urlfetch with 为了使其异步,我认为我应该使用urlfetch

    rpc = urlfetch.create_rpc()
    options = json.dumps({"channels": ["blabla"],
                          "data": {
                              "alert": "A",
                              "title": "B",
                              "badge": "Increment",
                              "category": "C",
                              "sound": "default",
                              "additionalInfos": {"X": "Custom dict"}},
                          })
    urlfetch.make_fetch_call(rpc, "https://api.parse.com/1/push:443", options)

But I cannot find examples how adding header..any suggestion? 但是我找不到示例如何添加标题..任何建议? Thanks! 谢谢!

     rpc = urlfetch.create_rpc(deadline=60)
     url =  "https://api.parse.com/1/push:443"
     request_params = {
                         "channels": ["blabla"],
                         "data": {
                         "alert": "A",
                         "title":"B",
                         "badge": "Increment",
                         "category": "C",
                         "sound": "default",
                         "additionalInfos": {"X": "Custom dict"}}
                       }
     headers = {
                  "X-Parse-Application-Id": parse_app_id,
                  "X-Parse-REST-API-Key": parse_rest_api_key,
                  "Content-Type": "application/json"
                }
     urlfetch.make_fetch_call(rpc,
                              url,
                              payload=json.dumps(request_params),
                              method=urlfetch.POST,
                              headers=headers)

You can always Look into the source if you need more help 如果您需要更多帮助,可以随时查看源代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM