mirror of
https://gitee.com/sui-feng-cb/AzurLaneAutoScript1
synced 2026-03-27 20:17:17 +08:00
Pref: Replace filelock with threading lock to reduce IO operations
This commit is contained in:
@@ -1,22 +1,20 @@
|
|||||||
import os
|
|
||||||
import sys
|
|
||||||
import queue
|
|
||||||
import argparse
|
import argparse
|
||||||
|
import os
|
||||||
|
import queue
|
||||||
import threading
|
import threading
|
||||||
from multiprocessing import Process
|
from multiprocessing import Process
|
||||||
from typing import Dict, List, Union
|
from typing import Dict, List, Union
|
||||||
|
|
||||||
import inflection
|
import inflection
|
||||||
from filelock import FileLock
|
|
||||||
from rich.console import Console, ConsoleRenderable
|
from rich.console import Console, ConsoleRenderable
|
||||||
|
|
||||||
# Since this file does not run under the same process or subprocess of app.py
|
# Since this file does not run under the same process or subprocess of app.py
|
||||||
# the following code needs to be repeated
|
# the following code needs to be repeated
|
||||||
# Import fake module before import pywebio to avoid importing unnecessary module PIL
|
# Import fake module before import pywebio to avoid importing unnecessary module PIL
|
||||||
from module.webui.fake_pil_module import *
|
from module.webui.fake_pil_module import *
|
||||||
|
|
||||||
import_fake_pil_module()
|
import_fake_pil_module()
|
||||||
|
|
||||||
from module.config.utils import filepath_config
|
|
||||||
from module.logger import logger, set_file_logger, set_func_logger
|
from module.logger import logger, set_file_logger, set_func_logger
|
||||||
from module.submodule.submodule import load_mod
|
from module.submodule.submodule import load_mod
|
||||||
from module.submodule.utils import get_available_func, get_available_mod, get_available_mod_func, get_config_mod, \
|
from module.submodule.utils import get_available_func, get_available_mod, get_available_mod_func, get_config_mod, \
|
||||||
@@ -34,6 +32,7 @@ class ProcessManager:
|
|||||||
self.renderables_max_length = 400
|
self.renderables_max_length = 400
|
||||||
self.renderables_reduce_length = 80
|
self.renderables_reduce_length = 80
|
||||||
self._process: Process = None
|
self._process: Process = None
|
||||||
|
self._process_locks: Dict[str, threading.Lock] = {}
|
||||||
self.thd_log_queue_handler: threading.Thread = None
|
self.thd_log_queue_handler: threading.Thread = None
|
||||||
|
|
||||||
def start(self, func, ev: threading.Event = None) -> None:
|
def start(self, func, ev: threading.Event = None) -> None:
|
||||||
@@ -64,7 +63,12 @@ class ProcessManager:
|
|||||||
self.thd_log_queue_handler.start()
|
self.thd_log_queue_handler.start()
|
||||||
|
|
||||||
def stop(self) -> None:
|
def stop(self) -> None:
|
||||||
lock = FileLock(f"{filepath_config(self.config_name)}.lock")
|
try:
|
||||||
|
lock = self._process_locks[self.config_name]
|
||||||
|
except KeyError:
|
||||||
|
lock = threading.Lock()
|
||||||
|
self._process_locks[self.config_name] = lock
|
||||||
|
|
||||||
with lock:
|
with lock:
|
||||||
if self.alive:
|
if self.alive:
|
||||||
self._process.kill()
|
self._process.kill()
|
||||||
|
|||||||
Reference in New Issue
Block a user