Add timeouts to engine processes

This commit is contained in:
Brett Williams
2023-05-30 12:09:09 -05:00
parent a668fa70e5
commit 9cebcb3c62
5 changed files with 22 additions and 15 deletions

View File

@@ -17,7 +17,7 @@ class Blender(BaseRenderEngine):
try:
render_path = cls.renderer_path()
if render_path:
ver_out = subprocess.check_output([render_path, '-v'])
ver_out = subprocess.check_output([render_path, '-v'], timeout=SUBPROCESS_TIMEOUT)
version = ver_out.decode('utf-8').splitlines()[0].replace('Blender', '').strip()
except Exception as e:
logger.error(f'Failed to get Blender version: {e}')
@@ -30,11 +30,11 @@ class Blender(BaseRenderEngine):
return formats
@classmethod
def run_python_expression(cls, project_path, python_expression):
def run_python_expression(cls, project_path, python_expression, timeout=None):
if os.path.exists(project_path):
try:
return subprocess.run([cls.renderer_path(), '-b', project_path, '--python-expr', python_expression],
capture_output=True)
capture_output=True, timeout=timeout)
except Exception as e:
logger.warning(f"Error running python expression in blender: {e}")
pass
@@ -42,11 +42,11 @@ class Blender(BaseRenderEngine):
raise FileNotFoundError(f'Project file not found: {project_path}')
@classmethod
def run_python_script(cls, project_path, script_path):
def run_python_script(cls, project_path, script_path, timeout=None):
if os.path.exists(project_path) and os.path.exists(script_path):
try:
return subprocess.run([cls.renderer_path(), '-b', project_path, '--python', script_path],
capture_output=True)
capture_output=True, timeout=timeout)
except Exception as e:
logger.warning(f"Error running python expression in blender: {e}")
pass
@@ -57,11 +57,11 @@ class Blender(BaseRenderEngine):
raise Exception("Uncaught exception")
@classmethod
def get_scene_info(cls, project_path):
def get_scene_info(cls, project_path, timeout=10):
scene_info = None
try:
results = cls.run_python_script(project_path, os.path.join(os.path.dirname(os.path.realpath(__file__)),
'scripts', 'get_blender_info.py'))
'scripts', 'get_blender_info.py'), timeout=timeout)
result_text = results.stdout.decode()
for line in result_text.splitlines():
if line.startswith('SCENE_DATA:'):
@@ -73,16 +73,17 @@ class Blender(BaseRenderEngine):
return scene_info
@classmethod
def pack_project_file(cls, project_path):
def pack_project_file(cls, project_path, timeout=30):
# Credit to L0Lock for pack script - https://blender.stackexchange.com/a/243935
pack_expression = "import bpy\n" \
"bpy.ops.file.pack_all()\n" \
"bpy.ops.file.make_paths_absolute()\n" \
"myPath = bpy.data.filepath\n" \
"myPath = str(myPath)\n" \
"bpy.ops.wm.save_as_mainfile(filepath=myPath[:-6]+'_packed'+myPath[-6:])"
try:
results = Blender.run_python_expression(project_path, pack_expression)
results = Blender.run_python_expression(project_path, pack_expression, timeout=timeout)
result_text = results.stdout.decode()
dir_name = os.path.dirname(project_path)