# mysql数据库常用任务函数封装 import pymysql, socket, time import config #公共连接库 def pymysqlAlias(): return pymysql.connect( host=config.mysql_gpu['host'], port=config.mysql_gpu['port'], user=config.mysql_gpu['user'], password=config.mysql_gpu['password'], db=config.mysql_gpu['db'], charset=config.mysql_gpu['charset'],) # 新增新的任务 def add_reapeat_texture_task(data): try: resExist = is_exist(data["pid"]) if resExist == True: return with pymysqlAlias() as conn: cursor = conn.cursor() sql = f'insert into task_repeat_texture (pid,heads,createTime) values ("{data["pid"]}","{data["heads"]}","{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}")' print(f'sql: {sql}') cursor.execute(sql) conn.commit() except Exception as e: print(f"{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())} 执行add_task({data})异常: {str(e)}") # def is_exist(pid): try: with pymysqlAlias() as conn: cursor = conn.cursor() sql = f'select count(*) from task_repeat_texture where pid = {pid} and status = 0' print(f'sql: {sql}') cursor.execute(sql) result = cursor.fetchone() if result[0] > 0: return True else: return False except Exception as e: print(f"{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())} 执行is_new_make_psid()异常: {str(e)}") return "error" def get_pid_task(): try: with pymysqlAlias() as conn: cursor = conn.cursor() sql = f'select * from task_repeat_texture where status = 1 limit 1' print(f'sql: {sql}') cursor.execute(sql) result = cursor.fetchone() if result: return result else: return None except Exception as e: print(f"{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())} 执行get_pid_task()异常: {str(e)}") return "error" #更新状态 def update_status(id,status): try: with pymysqlAlias() as conn: cursor = conn.cursor() hostname = socket.gethostname() sql = f'update task_repeat_texture set status = {status},updateTime = now() where id = {id}' print(f'开始任务sql: {sql}') cursor.execute(sql) conn.commit() except Exception as e: print(f"{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())} 执行update_status()异常: {str(e)}")