推送同一张卡的最佳方法,附带给我的玻璃器皿的许多用户

时间:2013-08-01 07:12:20

标签: python google-app-engine google-mirror-api

我正在尝试清理并放入类似prod的形状我的代码但是我遇到了来自Google App引擎的prod env的许多问题。 一切都在本地没有问题,但每次我尝试测试prod我有截止日期和超时问题。 我的问题是: 1)从我的玻璃器皿向许多用户推送带有SAME附件(视频)的SAME卡的最佳/建议做法是什么? 这是从队列中调用的类:

class batchWorker(webapp2.RequestHandler):
def post(self):
    userToPush = self.request.get('user_id')
    #posting video
    media_link = util.get_full_url(self, '/static/video/short_from_glass_low.mp4')
    resp = urlfetch.fetch(media_link, deadline=2000)
    media_video = MediaIoBaseUpload(io.BytesIO(resp.content), mimetype='video/mp4',
                                        resumable=False)
    users = Credentials.all()
    for user in users:
            creds = StorageByKeyName(Credentials, user.key().name(), 'credentials').get()
            mirror_service = util.create_service('mirror', 'v1', creds)
            #first card
            timeline_item01 = {'text':'New video from bundle - Test002'}
            timeline_item01['bundleId'] = 'video_001'
            timeline_item01['isBundleCover'] = 'true'
            mirror_service.timeline().insert(body=timeline_item01).execute()
            #second card
            timeline_item = {'text': 'Text here'}
            timeline_item['isBundleCover'] = 'false'
            timeline_item['bundleId'] = 'video_001'
            mirror_service.timeline().insert(body=timeline_item, media_body=media_video).execute()
            logging.info("Posted video for user %s" % user.key().name())

这就是我把它推到队列的方式:

taskqueue.add(url='/worker', params={'user_id': '103012621006129330069'})

有些时候我完成了这个,其他一些我得到了如下记录:

2013-08-01 07:15:37.695 /worker 500 6481ms 0kb AppEngine-Google; (+http://code.google.com/appengine)
I 2013-08-01 07:15:31.676 make: Got type <class 'google.appengine.api.datastore_types.Blob'>
I 2013-08-01 07:15:31.677 validate: Got type <class 'oauth2client.client.OAuth2Credentials'>
I 2013-08-01 07:15:31.711 make: Got type <class 'google.appengine.api.datastore_types.Blob'>
I 2013-08-01 07:15:31.712 validate: Got type <class 'oauth2client.client.OAuth2Credentials'>
I 2013-08-01 07:15:31.713 URL being requested: https://www.googleapis.com/discovery/v1/apis/mirror/v1/rest?userIp=0.1.0.2
I 2013-08-01 07:15:31.770 URL being requested: https://www.googleapis.com/mirror/v1/timeline?alt=json
I 2013-08-01 07:15:32.675 URL being requested: https://www.googleapis.com/upload/mirror/v1/timeline?uploadType=multipart&alt=json
E 2013-08-01 07:15:37.685 The API call urlfetch.Fetch() took too long to respond and was cancelled. Traceback (most recent call last): File "/base/data/home/runtimes/python27
其他一些我得到了这个:

2013-08-01 07:15:11.066 /worker 500 7239ms 0kb AppEngine-Google; (+http://code.google.com/appengine)
I 2013-08-01 07:15:04.434 make: Got type <class 'google.appengine.api.datastore_types.Blob'>
I 2013-08-01 07:15:04.439 validate: Got type <class 'oauth2client.client.OAuth2Credentials'>
I 2013-08-01 07:15:04.527 make: Got type <class 'google.appengine.api.datastore_types.Blob'>
I 2013-08-01 07:15:04.528 validate: Got type <class 'oauth2client.client.OAuth2Credentials'>
I 2013-08-01 07:15:04.529 URL being requested: https://www.googleapis.com/discovery/v1/apis/mirror/v1/rest?userIp=0.1.0.2
I 2013-08-01 07:15:04.587 URL being requested: https://www.googleapis.com/mirror/v1/timeline?alt=json
I 2013-08-01 07:15:04.620 Refreshing due to a 401
I 2013-08-01 07:15:04.628 make: Got type <class 'google.appengine.api.datastore_types.Blob'>
I 2013-08-01 07:15:04.629 validate: Got type <class 'oauth2client.client.OAuth2Credentials'>
I 2013-08-01 07:15:04.630 Refreshing access_token
I 2013-08-01 07:15:04.833 make: Got type <class 'google.appengine.api.datastore_types.Blob'>
I 2013-08-01 07:15:04.834 validate: Got type <class 'oauth2client.client.OAuth2Credentials'>
I 2013-08-01 07:15:04.839 validate: Got type <class 'oauth2client.client.OAuth2Credentials'>
I 2013-08-01 07:15:04.839 get: Got type <class 'model.Credentials'>
I 2013-08-01 07:15:05.970 URL being requested: https://www.googleapis.com/upload/mirror/v1/timeline?uploadType=multipart&alt=json
E 2013-08-01 07:15:10.985 Deadline exceeded while waiting for HTTP response from URL: https://www.googleapis.com/upload/mirror/v1/timeline?uploadType=multipart&alt=json Traceba

目前我很沮丧,因为我可以100%理解逻辑,但刺激实施是一种痛苦。

我是否需要使用批次电话才能一个接一个地推送卡片? 有不同的方法吗? 为什么我在队列进程应该有10分钟的截止日期时遇到截止日期错误(截止日期错误在执行10秒后出现)。

2 个答案:

答案 0 :(得分:2)

超时实际上来自您的上传请求,您可以通过修改创建服务实例(util.create_service)的代码来修复此问题,并在实例化timeout时设置httplib2.Http参数对象:

# Instantiate an Http instance
http = httplib2.Http(timeout=2000) # Use a bigger value if you want to stay safe.

关于批量处理请求,遗憾的是,API不支持混合批量和媒体上传请求......

答案 1 :(得分:0)

我认为这一行:

 resp = urlfetch.fetch(media_link, deadline=2000)

正在尝试为该请求设置2000秒的截止日期,但是:

从文档:最长期限(请求处理程序)60秒 因此,您的截止日期将被忽略或恢复为默认值。