定时读取redis 超时问题处理

This commit is contained in:
dongchangxi committed 2024-12-27 14:56:45 +08:00
1 parent 8dddd926d8
commit 8184799610
3 files changed
+64 -4

No files matched your search

+20 -1
View File
@@ -171,6 +171,19 @@ def createGlb(pid):
export_and_update_glbs(pid)
def create_redis_connection():
"""创建 Redis 连接,若连接失败则重试"""
while True:
try:
r = redis.Redis(host="106.14.158.208",password="kcV2000",port=6379,db=6)
# 尝试进行一次操作,检查连接是否有效
r.ping() # ping 操作是一个简单的连接测试
print("Redis连接成功!")
return r
except ConnectionError:
print("Redis连接失败,正在重试...")
time.sleep(5)
if __name__ == '__main__':
arrArgs = sys.argv
if len(arrArgs) == 2:
@@ -181,8 +194,9 @@ if __name__ == '__main__':
createGlb(str(pid))
else:
r = redis.Redis(host="106.14.158.208",password="kcV2000",port=6379,db=6)
r = create_redis_connection()
while True:
try:
if r.llen('model:3dglb') == 0:
print('队列为空,等待10秒')
time.sleep(10)
@@ -198,5 +212,10 @@ if __name__ == '__main__':
continue
createGlb(pid)
except Exception as e:
print(f'错误:{e}')
time.sleep(10)
r = create_redis_connection()
continue
+22 -2
View File
@@ -22,9 +22,24 @@ def getOrderInfoByOrderId(orderId):
print(f"获取订单信息异常: {str(e)}")
return "error","error"
if __name__ == '__main__':
r = redis.Redis(host="106.14.158.208",password="kcV2000",port=6379,db=6)
def create_redis_connection():
"""创建 Redis 连接,若连接失败则重试"""
while True:
try:
r = redis.Redis(host="106.14.158.208",password="kcV2000",port=6379,db=6)
# 尝试进行一次操作,检查连接是否有效
r.ping() # ping 操作是一个简单的连接测试
print("Redis连接成功!")
return r
except ConnectionError:
print("Redis连接失败,正在重试...")
time.sleep(5)
if __name__ == '__main__':
r = create_redis_connection()
while True:
try:
print(f"时间-{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}")
if r.llen('model:weight') == 0:
print('队列为空,等待10秒')
@@ -72,3 +87,8 @@ if __name__ == '__main__':
print(f'执行脚本--- "python D:/make2/tools/cal_weight.py {typeModel} {pid} {modelSize} {orderId}"')
#发起计算请求
os.system(f"python D:/make2/tools/cal_weight.py {typeModel} {pid} {modelSize} {orderId}")
except Exception as e:
print(f"执行脚本异常: {str(e)}")
time.sleep(10)
r = create_redis_connection()
continue
+22 -1
View File
@@ -42,9 +42,24 @@ def zip_dir(path, output=None):
zip.write(os.path.join(root, filename), relative_root + filename) # 文件路径 压缩文件路径(相对路径)
zip.close()
def create_redis_connection():
"""创建 Redis 连接,若连接失败则重试"""
while True:
try:
r = redis.Redis(host="106.14.158.208",password="kcV2000",port=6379,db=1)
# 尝试进行一次操作,检查连接是否有效
r.ping() # ping 操作是一个简单的连接测试
print("Redis连接成功!")
return r
except ConnectionError:
print("Redis连接失败,正在重试...")
time.sleep(5)
def main():
#连接mp 的redis
r = redis.Redis(host="106.14.158.208",password="kcV2000",port=6379,db=1)
r = create_redis_connection()
#本地测试环境
#r = redis.Redis(host="172.31.1.254",port=6379,db=1)
#取出数据
@@ -131,5 +146,11 @@ if __name__ == '__main__':
#开启定时循环模式
print("开启定时任务,检测是否打包")
while True:
try:
main()
time.sleep(10)
continue
except Exception as e:
print(f"出现异常: {str(e)}")
time.sleep(10)
continue