mirror of
https://gitee.com/sui-feng-cb/AzurLaneAutoScript1
synced 2026-03-31 12:20:33 +08:00
Opt: Add file lock
This commit is contained in:
2
gui.py
2
gui.py
@@ -71,7 +71,9 @@ class Alas:
|
|||||||
toast(t("Gui.Toast.AlasIsRunning"), position='right', color='warn')
|
toast(t("Gui.Toast.AlasIsRunning"), position='right', color='warn')
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
|
if self.process.is_alive():
|
||||||
self.process.terminate()
|
self.process.terminate()
|
||||||
|
if self.thd_log_queue_handler.is_alive():
|
||||||
self.thd_log_queue_handler.stop()
|
self.thd_log_queue_handler.stop()
|
||||||
|
|
||||||
def _thread_log_queue_handler(self):
|
def _thread_log_queue_handler(self):
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import os
|
|||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
from datetime import datetime, timedelta, timezone
|
from datetime import datetime, timedelta, timezone
|
||||||
|
from filelock import FileLock
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
@@ -66,7 +67,6 @@ def read_file(file):
|
|||||||
Returns:
|
Returns:
|
||||||
dict, list:
|
dict, list:
|
||||||
"""
|
"""
|
||||||
print(f'read: {file}')
|
|
||||||
folder = os.path.dirname(file)
|
folder = os.path.dirname(file)
|
||||||
if not os.path.exists(folder):
|
if not os.path.exists(folder):
|
||||||
os.mkdir(folder)
|
os.mkdir(folder)
|
||||||
@@ -75,6 +75,9 @@ def read_file(file):
|
|||||||
return {}
|
return {}
|
||||||
|
|
||||||
_, ext = os.path.splitext(file)
|
_, ext = os.path.splitext(file)
|
||||||
|
lock = FileLock(f"{file}.lock")
|
||||||
|
with lock:
|
||||||
|
print(f'read: {file}')
|
||||||
if ext == '.yaml':
|
if ext == '.yaml':
|
||||||
with open(file, mode='r', encoding='utf-8') as f:
|
with open(file, mode='r', encoding='utf-8') as f:
|
||||||
s = f.read()
|
s = f.read()
|
||||||
@@ -99,12 +102,14 @@ def write_file(file, data):
|
|||||||
file (str):
|
file (str):
|
||||||
data (dict, list):
|
data (dict, list):
|
||||||
"""
|
"""
|
||||||
print(f'write: {file}')
|
|
||||||
folder = os.path.dirname(file)
|
folder = os.path.dirname(file)
|
||||||
if not os.path.exists(folder):
|
if not os.path.exists(folder):
|
||||||
os.mkdir(folder)
|
os.mkdir(folder)
|
||||||
|
|
||||||
_, ext = os.path.splitext(file)
|
_, ext = os.path.splitext(file)
|
||||||
|
lock = FileLock(f"{file}.lock")
|
||||||
|
with lock:
|
||||||
|
print(f'write: {file}')
|
||||||
if ext == '.yaml':
|
if ext == '.yaml':
|
||||||
with open(file, mode='w', encoding='utf-8') as f:
|
with open(file, mode='w', encoding='utf-8') as f:
|
||||||
if isinstance(data, list):
|
if isinstance(data, list):
|
||||||
|
|||||||
Reference in New Issue
Block a user