winziprint.py: Add feature to abort jobs
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
.idea
|
||||
__pycache__
|
||||
*.exe
|
||||
*.spec
|
||||
|
||||
@@ -4,4 +4,5 @@ pyinstaller ^
|
||||
--console ^
|
||||
--icon "NONE" ^
|
||||
--name "WinziPrint" ^
|
||||
--clean ^
|
||||
src/winziprint.py
|
||||
|
||||
@@ -11,16 +11,19 @@ import socketserver
|
||||
import io
|
||||
import signal
|
||||
import math
|
||||
import threading
|
||||
|
||||
import weasyprint
|
||||
import pypdf
|
||||
|
||||
|
||||
VERSION = __version__ = '0.2.9'
|
||||
VERSION = __version__ = '0.3.0'
|
||||
SOCKET_ADDRESS = ('127.0.0.1', 30983)
|
||||
|
||||
BATCH_SIZE = 10
|
||||
|
||||
RUNNING: dict[int, bool] = {}
|
||||
|
||||
|
||||
def convert_part(output_file: str, batch: list[str], step_cb: Callable, encoding: str = None) -> list[int]:
|
||||
documents = []
|
||||
@@ -55,6 +58,8 @@ def convert(input_files: list[str],
|
||||
steps = [0]
|
||||
|
||||
def next_step() -> None:
|
||||
if not RUNNING[threading.get_ident()]:
|
||||
raise InterruptedError('aborted')
|
||||
steps[0] += 1
|
||||
if progress:
|
||||
print(f'progress: {steps[0]}/{total_steps}', file=out, flush=True)
|
||||
@@ -233,12 +238,23 @@ def _get_arg(args: list[str], n1: str, n2: str = None, flag: bool = False) -> No
|
||||
class ConnectionHandler(socketserver.StreamRequestHandler):
|
||||
def handle(self):
|
||||
try:
|
||||
while True:
|
||||
out = io.TextIOWrapper(self.wfile, encoding='utf-8')
|
||||
for line in io.TextIOWrapper(self.rfile, encoding='utf-8'):
|
||||
_wrapper_convert(line.strip().split(';'), out=out)
|
||||
except ValueError:
|
||||
out = io.TextIOWrapper(self.wfile, encoding='utf-8')
|
||||
RUNNING[threading.get_ident()] = True
|
||||
print(f'id: {threading.get_ident()}', file=out, flush=True)
|
||||
for line in io.TextIOWrapper(self.rfile, encoding='utf-8'):
|
||||
if not RUNNING[threading.get_ident()]:
|
||||
print(f'error: aborted', file=out, flush=True)
|
||||
break
|
||||
parts = line.strip().split(';')
|
||||
if len(parts) == 2 and parts[0] == 'cancel':
|
||||
thread_id = int(parts[1].strip())
|
||||
RUNNING[thread_id] = False
|
||||
continue
|
||||
_wrapper_convert(parts, out=out)
|
||||
except (ValueError, ConnectionError):
|
||||
pass # socket closed by client
|
||||
finally:
|
||||
del RUNNING[threading.get_ident()]
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
||||
Reference in New Issue
Block a user