refactor async calls

This commit is contained in:
j 2014-07-09 20:59:24 +02:00
parent e5a75b41a2
commit 2ce541d211

View file

@ -596,15 +596,7 @@ class Client(object):
post['files'] = files['files'] post['files'] = files['files']
post['volume'] = name post['volume'] = name
print 'sending list of files in %s (%s total)' % (name, len(post['files'])) print 'sending list of files in %s (%s total)' % (name, len(post['files']))
r = self.api.update(post) r = self.api.async('update', post)
if r['status']['code'] == 200:
#backend works on update request asyncronously, wait for it to finish
if 'taskId' in r['data']:
t = self.api.taskStatus(task_id=r['data']['taskId'])
print 'waiting for server ...'
while t['data']['status'] == 'PENDING':
time.sleep(5)
t = self.api.taskStatus(task_id=r['data']['taskId'])
#send empty list to get updated list of requested info/files/data #send empty list to get updated list of requested info/files/data
post = {'info': {}} post = {'info': {}}
r = self.api.update(post) r = self.api.update(post)
@ -718,13 +710,7 @@ class Client(object):
print 'sending info for %d files' % len(info) print 'sending info for %d files' % len(info)
post = {'info': {}, 'upload': True} post = {'info': {}, 'upload': True}
post['info'] = self.get_info_for_ids(info, prefix) post['info'] = self.get_info_for_ids(info, prefix)
r = self.api.update(post) r = self.api.async('update', post)
if 'taskId' in r['data']:
t = self.api.taskStatus(task_id=r['data']['taskId'])
print 'waiting for server ...'
while t['data']['status'] == 'PENDING':
time.sleep(5)
t = self.api.taskStatus(task_id=r['data']['taskId'])
#send empty list to get updated list of requested info/files/data #send empty list to get updated list of requested info/files/data
post = {'info': {}} post = {'info': {}}
r = self.api.update(post) r = self.api.update(post)
@ -908,6 +894,18 @@ class API(ox.API):
netloc = urlparse(self.url).netloc netloc = urlparse(self.url).netloc
self._resume_file = '/tmp/pandora_client.%s.%s.json' % (os.environ.get('USER'), netloc) self._resume_file = '/tmp/pandora_client.%s.%s.json' % (os.environ.get('USER'), netloc)
def async(self, action, data, interval=5):
t = r = getattr(self, action)(data)
if r['status']['code'] == 200:
# wait for async task to finish
if 'taskId' in r['data']:
t = self.taskStatus(task_id=r['data']['taskId'])
print 'waiting for server ...'
while t['data'].get('status') == 'PENDING':
time.sleep(interval)
t = self.taskStatus(task_id=r['data']['taskId'])
return t
def uploadFrames(self, i, data): def uploadFrames(self, i, data):
#upload frames #upload frames
if self.site['media'].get('importFrames') and i['frames']: if self.site['media'].get('importFrames') and i['frames']: