您的位置:首页 > 运维架构

oslo_service loopingcall的使用

2016-07-27 10:02 1346 查看
loopingcall用于专门检测某个task是否完成了,比sleep优雅多了

from oslo_service import loopingcall

def wait_del_backup():
start_time = int(time.time())
timeout = 120
del_backup = cinder_client.backups.list(
search_opts={'id': backup_id})
if len(del_backup) == 0:
logging.info("Delete backup %s complete" % backup_id)
raise loopingcall.LoopingCallDone()
if del_backup[0].status in ['error', 'error_deleting']:
raise Exception("Delete backup %s failed, "
"the status of backup is %s."
% (backup_id, del_backup[0].status))
if (del_backup[0].status == 'deleting') and (int(time.time()) -\
start_time > timeout):
logging.error("Delete backup %s failed, In a state of "
"deleting over 120s")
raise

timer = loopingcall.FixedIntervalLoopingCall(wait_del_backup)
timer.start(interval=0.5).wait()


具体使用看源码:https://github.com/openstack/oslo.service/blob/master/oslo_service/loopingcall.py
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  oslo service