Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

辞書が壊れてエンジンが起動しなくなる #623 の解決 #624

Merged
merged 4 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions voicevox_engine/user_dict.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import shutil
import sys
import threading
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Dict, List, Optional
Expand All @@ -13,7 +14,7 @@

from .model import UserDictWord, WordTypes
from .part_of_speech_data import MAX_PRIORITY, MIN_PRIORITY, part_of_speech_data
from .utility import delete_file, engine_root, get_save_dir
from .utility import delete_file, engine_root, get_save_dir, mutex_wrapper

root_dir = engine_root()
save_dir = get_save_dir()
Expand All @@ -26,6 +27,11 @@
compiled_dict_path = save_dir / "user.dic"


mutex_user_dict = threading.Lock()
mutex_openjtalk_dict = threading.Lock()


@mutex_wrapper(mutex_user_dict)
def write_to_json(user_dict: Dict[str, UserDictWord], user_dict_path: Path):
converted_user_dict = {}
for word_uuid, word in user_dict.items():
Expand All @@ -40,6 +46,7 @@ def write_to_json(user_dict: Dict[str, UserDictWord], user_dict_path: Path):
user_dict_path.write_text(user_dict_json, encoding="utf-8")


@mutex_wrapper(mutex_openjtalk_dict)
def update_dict(
default_dict_path: Path = default_dict_path,
user_dict_path: Path = user_dict_path,
Expand Down Expand Up @@ -97,6 +104,7 @@ def update_dict(
pyopenjtalk.set_user_dict(str(compiled_dict_path.resolve(strict=True)))


@mutex_wrapper(mutex_user_dict)
def read_dict(user_dict_path: Path = user_dict_path) -> Dict[str, UserDictWord]:
if not user_dict_path.is_file():
return {}
Expand All @@ -114,7 +122,7 @@ def read_dict(user_dict_path: Path = user_dict_path) -> Dict[str, UserDictWord]:
del word["cost"]
result[str(UUID(word_uuid))] = UserDictWord(**word)

return result
return result


def create_word(
Expand Down
2 changes: 2 additions & 0 deletions voicevox_engine/utility/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
connect_base64_waves,
decode_base64_waves,
)
from .mutex_utility import mutex_wrapper
from .path_utility import delete_file, engine_root, get_save_dir

__all__ = [
Expand All @@ -12,4 +13,5 @@
"delete_file",
"engine_root",
"get_save_dir",
"mutex_wrapper",
]
15 changes: 15 additions & 0 deletions voicevox_engine/utility/mutex_utility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import threading


def mutex_wrapper(lock: threading.Lock):
def wrap(f):
def func(*args, **kw):
lock.acquire()
try:
return f(*args, **kw)
finally:
lock.release()

return func

return wrap