PyInstaller打包JIRA自动派单程序后无法循环运行问题排查
问题原因及修复方案
核心原因
未捕获的异常直接终止程序
你的check_issue()函数中,如果jira_login()登录失败(比如打包后环境网络异常、依赖缺失导致JIRA实例初始化失败),jira会被设为空字符串。后续调用jira.search_issues()会触发AttributeError,但函数没有任何异常捕获逻辑,程序直接崩溃退出,根本走不到time.sleep和递归调用那一步。而PyCharm运行时环境正常,登录成功,所以没触发这个问题。递归循环的栈溢出隐患
用递归调用check()实现循环,会持续占用栈内存。短期运行可能没问题,但跑久了会触发Python默认的递归深度限制(约1000层),直接导致程序崩溃。
修复步骤
1. 给check_issue()加异常防护
先判断JIRA登录是否成功,再执行后续逻辑,同时捕获查询时的异常,确保程序不会直接挂掉:
def check_issue(): global lst_ignore jira = jira_login() # 接收登录返回的实例,避免依赖全局变量 if not jira: print("JIRA登录失败,跳过本次检查") return size = 100000 start = 0 * 100000 search_query = 'status not in (Done, Closed,Canceled) and assignee is EMPTY and project = "Success" and reporter not in (57f778:f48131cb-b67d-43c7-b30d-2b58d98bd077)' try: issues = jira.search_issues(search_query, start, size) for issue in issues: if issue not in lst_ignore: winsound.Beep(2000, 1000) issue.update(assignee={'accountId': '60f1d072a0de61930ad83770'}) print(f"{issue} : 已分配给你!") lst_ignore.append(issue) except Exception as e: print(f"检查JIRA任务出错: {e}")
2. 把递归改成迭代循环
用while True替换递归,彻底避免栈溢出问题:
def check(): while True: check_issue() time.sleep(5)
3. 优化PyInstaller打包命令
打包时要确保JIRA相关依赖都被包含,用以下命令打包:
pyinstaller --onefile --hidden-import=jira --hidden-import=requests your_script_name.py
如果还是有问题,可以生成spec文件,手动指定所有需要的依赖。
修改后的完整代码
import time import winsound from jira import JIRA lst_ignore = [] def jira_login(): user = 'user@cloud.com' token = 'kj432hj43214YMzCyMLOe7682' jira = None try: options = {'server': 'https://cloud.atlassian.net'} jira = JIRA(options=options, basic_auth=(user, token)) except Exception as e: if '401' in str(e): print("登录JIRA失败,请检查用户名和密码", e) else: print("JIRA登录出错:", e) return jira def check_issue(): global lst_ignore jira = jira_login() if not jira: print("JIRA未登录成功,跳过本次检查") return size = 100000 start = 0 * 100000 search_query = 'status not in (Done, Closed,Canceled) and assignee is EMPTY and project = "Success" and reporter not in (57f778:f48131cb-b67d-43c7-b30d-2b58d98bd077)' try: issues = jira.search_issues(search_query, start, size) for issue in issues: if issue not in lst_ignore: winsound.Beep(2000, 1000) issue.update(assignee={'accountId': '60f1d072a0de61930ad83770'}) print(f"{issue} : 已分配给你!") lst_ignore.append(issue) except Exception as e: print(f"检查任务时发生错误: {e}") def check(): while True: check_issue() time.sleep(5) if __name__ == '__main__': check()
内容的提问来源于stack exchange,提问作者pyrazs
相关产品推荐
相关产品推荐

