如何解决Python - 重试功能
在代码中,我多次调用在远程数据库上执行不同任务的函数, 如果我正在运行一个调用,在某些情况下返回代码为 http 代码 = 202(“接受”), 所以它也需要在“重试”模式下调用其他函数完成的任务,
代码:
class Trg:
def __init__(self,**kwargs):
self.__dict__.update(kwargs)
self.kwargs = kwargs
self.trg_cb_connention = Cluster("http://"+ self._checkIfIP(self.TARGET_CB_DB)+ ":8091",ClusterOptions(PasswordAuthenticator(self.TARGET_USER,self.TARGET_PASSWORD)))
.....
.....
def _ConnectToBucket(self):
try:
bucket = self.trg_cb_connention.bucket(self.trg_bucket_name)
#means bucket exist,otherwise create
return True
except Exception as e:
#bucket not exist,creating
self._CreateBucket()
def _CreateBucket(self):
try:
self.bm = self.trg_cb_connention.buckets()
response = self.bm.create_bucket(CreateBucketSettings(name=self.trg_bucket_name,bucket_type="couchbase",ram_quota_mb=100))
if response.http_status == 202: #acepected
#means "acepected:"
#Now call to function which validate the bucket created
self.retry_call(self._CheckBucketCreated())
else:
print("to check:",response)
def retry_call(function_name,args=None,kwargs=None,retries=300):
pass_on_args = args if args else []
pass_on_kwargs = kwargs if kwargs else {}
for index in range(1,retries+1):
try:
return function_name(*pass_on_args,**pass_on_kwargs)
except Exception as error:
if index == retries:
print("Failed %s attempts at calling: %s",retries,function_name)
raise
else:
print("Failed %d attempt(s) at calling: %s",index,function_name)
print("retrying")
失败原因:
Failed %d attempt(s) at calling: %s 1 MIG_ilceosp089
....
.......
........
retrying
Failed %s attempts at calling: %s 300 MIG_ilceosp089
Error 'Trg' object is not callable
首先,为什么会失败? 第二 - 不确定我是否做得对,我很好奇这种操作的正确方法是什么。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。