PyInstaller打包exe后无法终止Blender子进程的问题求助
PyInstaller打包后无法终止Blender子进程问题
直接运行Python脚本时,以下几种终止Blender子进程的方式均有效,但通过PyInstaller将脚本打包为.exe文件后,Blender进程无法被终止。
该代码属于多进程脚本的一部分,通过queue进行进程间通信。主程序关闭时需要终止Blender进程,否则主窗口会因等待“kill successful”消息陷入循环。我还尝试了psutil,但同样无效,目前不知如何解决。
初始尝试代码
import os import subprocess import queue import time bpy_script_path = os.path.join(main_dir, 'Packages', 'Scripts', 'BlenderRendering', 'bpy_script.py') blender_command = [blender_executable, '--background','--python', bpy_script_path] if os.name == 'nt': CREATE_NO_WINDOW = 0x08000000 blender_process = subprocess.Popen(blender_command, creationflags = CREATE_NO_WINDOW) else: blender_process = subprocess.Popen(blender_command) while blender_process.poll() == None: try: msg = queue.get_nowait() if msg[0] == 'kill': # 第一种尝试:kill进程 blender_process.kill() blender_process.wait() # 第二种尝试:terminate进程 blender_process.terminate() blender_process.wait() # 第三种尝试:用taskkill强制终止 subprocess.call(['taskkill', '/F', '/T', '/PID', str(blender_process.pid)]) queue.put(('kill successful', None, None)) return False except: pass time.sleep(1)
简化后的尝试代码
while blender_process.poll() == None: msg = None try: msg = queue.get_nowait() except: pass if msg: if msg[0] == 'kill': subprocess.call(['taskkill', '/F', '/T', '/PID', str(blender_process.pid)]) queue.put(('kill successful', None, None)) return False time.sleep(1)
内容的提问来源于stack exchange,提问作者MasterOfDesaster42
相关产品推荐
相关产品推荐

