From 8184799610f4b9a975e1859fd44cb3e066efcaed Mon Sep 17 00:00:00 2001 From: dongchangxi <458593490@qq.com> Date: Fri, 27 Dec 2024 14:56:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E6=97=B6=E8=AF=BB=E5=8F=96redis=20?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E9=97=AE=E9=A2=98=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- timer/external_order_glb.py | 45 ++++++++++---- timer/get_weight_to_update.py | 108 ++++++++++++++++++++-------------- timer/setup_oem_exe.py | 27 ++++++++- 3 files changed, 120 insertions(+), 60 deletions(-) diff --git a/timer/external_order_glb.py b/timer/external_order_glb.py index 365ec4f..497aa72 100644 --- a/timer/external_order_glb.py +++ b/timer/external_order_glb.py @@ -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,22 +194,28 @@ 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: - if r.llen('model:3dglb') == 0: - print('队列为空,等待10秒') - time.sleep(10) - continue + try: + if r.llen('model:3dglb') == 0: + print('队列为空,等待10秒') + time.sleep(10) + continue + + info = r.lpop('model:3dglb') + if info is None: + print('队列为空,等待10秒') + time.sleep(10) + pid = info.decode('utf-8') + + if not pid.isdigit(): + continue - info = r.lpop('model:3dglb') - if info is None: - print('队列为空,等待10秒') + createGlb(pid) + except Exception as e: + print(f'错误:{e}') time.sleep(10) - pid = info.decode('utf-8') - - if not pid.isdigit(): + r = create_redis_connection() continue - - createGlb(pid) diff --git a/timer/get_weight_to_update.py b/timer/get_weight_to_update.py index cf854da..ff5d68e 100644 --- a/timer/get_weight_to_update.py +++ b/timer/get_weight_to_update.py @@ -22,53 +22,73 @@ def getOrderInfoByOrderId(orderId): print(f"获取订单信息异常: {str(e)}") return "error","error" +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 = redis.Redis(host="106.14.158.208",password="kcV2000",port=6379,db=6) + r = create_redis_connection() while True: - print(f"时间-{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}") - if r.llen('model:weight') == 0: - print('队列为空,等待10秒') - time.sleep(10) - continue - - # info 的数据格式为有以下几种情况 1. orderId 2 orderId_print_9,1.2,45,27 3. orderId_auto_9,1.2,45,27 - info = r.lpop('model:weight') - if info is None: - print('队列为空,等待10秒') - time.sleep(10) - info = info.decode('utf-8') - orderId = 0 - pid = 0 - modelSize = [] - typeModel = "print" - #判断info是什么类型的数据 - arrInfo = info.split("_") - if len(arrInfo) == 1: - orderId = arrInfo[0] - pid,modelSize = getOrderInfoByOrderId(orderId) - if pid == "error": - print("获取订单信息失败") + try: + print(f"时间-{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}") + if r.llen('model:weight') == 0: + print('队列为空,等待10秒') + time.sleep(10) continue + + # info 的数据格式为有以下几种情况 1. orderId 2 orderId_print_9,1.2,45,27 3. orderId_auto_9,1.2,45,27 + info = r.lpop('model:weight') + if info is None: + print('队列为空,等待10秒') + time.sleep(10) + info = info.decode('utf-8') + orderId = 0 + pid = 0 + modelSize = [] + typeModel = "print" + #判断info是什么类型的数据 + arrInfo = info.split("_") + if len(arrInfo) == 1: + orderId = arrInfo[0] + pid,modelSize = getOrderInfoByOrderId(orderId) + if pid == "error": + print("获取订单信息失败") + continue - elif len(arrInfo) == 3: - orderId = arrInfo[0] - pid,temp = getOrderInfoByOrderId(orderId) - if pid == "error": - print("获取订单信息失败") - continue - typeModel = arrInfo[1] - modelSize = arrInfo[2] - if typeModel != "print" and typeModel != "auto": - print("typeModel类型不对") + elif len(arrInfo) == 3: + orderId = arrInfo[0] + pid,temp = getOrderInfoByOrderId(orderId) + if pid == "error": + print("获取订单信息失败") + continue + typeModel = arrInfo[1] + modelSize = arrInfo[2] + if typeModel != "print" and typeModel != "auto": + print("typeModel类型不对") + continue + + + if int(orderId) == 0 or int(pid) == 0 or len(modelSize) == 0: + print("orderId pid modelSize 有出现问题") continue - - - if int(orderId) == 0 or int(pid) == 0 or len(modelSize) == 0: - print("orderId pid modelSize 有出现问题") - continue - #转成数组 - arrModelSize = modelSize.split(",") - 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}") + #转成数组 + arrModelSize = modelSize.split(",") + 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 diff --git a/timer/setup_oem_exe.py b/timer/setup_oem_exe.py index 0221897..8ff2799 100644 --- a/timer/setup_oem_exe.py +++ b/timer/setup_oem_exe.py @@ -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: - main() - time.sleep(10) \ No newline at end of file + try: + main() + time.sleep(10) + continue + except Exception as e: + print(f"出现异常: {str(e)}") + time.sleep(10) + continue \ No newline at end of file