Allow preview to cancel!

This commit is contained in:
Julian Stirling 2025-08-12 15:35:39 +01:00
parent 7ea92ad55f
commit 05644456e0
2 changed files with 17 additions and 10 deletions

View file

@ -9,6 +9,7 @@ Interpreter Lock (GIL). May be possible to shift to multiprocessing in the futur
from typing import Optional, Any from typing import Optional, Any
import threading import threading
import subprocess import subprocess
import signal
import os import os
from io import TextIOWrapper from io import TextIOWrapper
import shlex import shlex
@ -160,11 +161,19 @@ class PreviewStitcher(BaseStitcher):
return True return True
return False return False
def wait(self) -> None: def wait(self, cancel: lt.deps.CancelHook) -> None:
"""Wait for this preview stitch to return.""" """Wait for this preview stitch to return.
if self.running:
with self._popen_lock: :raises InvocationCancelledError: if the action is cancelled.
self._popen_obj.wait() """
while self.running:
try:
cancel.sleep(0.1)
except lt.exceptions.InvocationCancelledError as e:
with self._popen_lock:
if self._popen_obj is not None:
self._popen_obj.send_signal(signal.SIGKILL)
raise (e)
class FinalStitcher(BaseStitcher): class FinalStitcher(BaseStitcher):
@ -272,10 +281,8 @@ class FinalStitcher(BaseStitcher):
) -> None: ) -> None:
"""Run the final stitch logging any output. """Run the final stitch logging any output.
Raises: :raises ChildProcessError: if exit code is not zero
ChildProcessError if exit code is not zero :raises InvocationCancelledError: if the action is cancelled.
InvocationCancelledError if the action is cancelled.
""" """
cmd = self.command cmd = self.command
self.logger.debug(f"Running command in subprocess: `{' '.join(cmd)}`") self.logger.debug(f"Running command in subprocess: `{' '.join(cmd)}`")

View file

@ -466,7 +466,7 @@ class SmartScanThing(lt.Thing):
self._scan_logger.info("Waiting for background processes to finish...") self._scan_logger.info("Waiting for background processes to finish...")
if self._preview_stitcher is not None: if self._preview_stitcher is not None:
self._preview_stitcher.wait() self._preview_stitcher.wait(self._cancel)
if self._scan_data.stitch_automatically: if self._scan_data.stitch_automatically:
self._scan_logger.info("Stitching final image (may take some time)...") self._scan_logger.info("Stitching final image (may take some time)...")