loopingcall用於專門檢測某個task是否完成了,比sleep優雅多了
python
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 git