|
| 1 | +"""Multiprocessing worker functions for cx_Freeze compatibility. |
| 2 | +
|
| 3 | +This module contains worker functions that are called by separate processes. |
| 4 | +Having them in a separate module ensures they can be properly imported |
| 5 | +by child processes when the application is frozen with cx_Freeze. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import logging |
| 11 | +import multiprocessing |
| 12 | +from typing import TYPE_CHECKING, Any, Callable |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + from multiprocessing import Queue |
| 16 | + |
| 17 | + |
| 18 | +def setup_worker_logging(log_level: str) -> None: |
| 19 | + """Set up logging for worker processes. |
| 20 | +
|
| 21 | + Args: |
| 22 | + log_level: The logging level to set |
| 23 | + """ |
| 24 | + # Configure logging for the worker process |
| 25 | + level = getattr(logging, log_level.upper(), logging.INFO) |
| 26 | + logging.basicConfig( |
| 27 | + level=level, |
| 28 | + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +def run_task_worker( |
| 33 | + task: Callable, result_queue: Queue, cancel_flag, *args: Any, **kwargs: Any |
| 34 | +) -> None: |
| 35 | + """Run a task in a separate process. |
| 36 | +
|
| 37 | + Args: |
| 38 | + task: The task to run |
| 39 | + result_queue: The queue to store the task result |
| 40 | + cancel_flag: The flag to indicate if the task should be cancelled |
| 41 | + *args: The task arguments |
| 42 | + **kwargs: The task keyword arguments |
| 43 | + """ |
| 44 | + try: |
| 45 | + # Set up logging if log_level is provided |
| 46 | + if "log_level" in kwargs: |
| 47 | + setup_worker_logging(kwargs["log_level"]) |
| 48 | + |
| 49 | + # Add required parameters to kwargs |
| 50 | + kwargs["result_queue"] = result_queue |
| 51 | + kwargs["cancel_flag"] = cancel_flag |
| 52 | + |
| 53 | + # Execute the task |
| 54 | + result = task(*args, **kwargs) |
| 55 | + |
| 56 | + # Return result if not cancelled |
| 57 | + if not cancel_flag.value: |
| 58 | + result_queue.put(("result", result)) |
| 59 | + except Exception as e: |
| 60 | + import traceback |
| 61 | + |
| 62 | + error_trace = traceback.format_exc() |
| 63 | + result_queue.put(("error", f"{str(e)}\n{error_trace}")) |
| 64 | + |
| 65 | + |
| 66 | +def start_worker_process( |
| 67 | + task: Callable, result_queue: Queue, cancel_flag, *args: Any, **kwargs: Any |
| 68 | +) -> multiprocessing.Process: |
| 69 | + """Start a worker process to run a task. |
| 70 | +
|
| 71 | + Args: |
| 72 | + task: The task to run |
| 73 | + result_queue: The queue to store the task result |
| 74 | + cancel_flag: The flag to indicate if the task should be cancelled |
| 75 | + *args: The task arguments |
| 76 | + **kwargs: The task keyword arguments |
| 77 | +
|
| 78 | + Returns: |
| 79 | + The started process |
| 80 | + """ |
| 81 | + process = multiprocessing.Process( |
| 82 | + target=run_task_worker, |
| 83 | + args=(task, result_queue, cancel_flag, *args), |
| 84 | + kwargs=kwargs, |
| 85 | + ) |
| 86 | + process.daemon = True |
| 87 | + process.start() |
| 88 | + return process |
| 89 | + |
| 90 | + |
| 91 | +# Required for cx_Freeze multiprocessing support |
| 92 | +if __name__ == "__main__": |
| 93 | + multiprocessing.freeze_support() |
0 commit comments