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

[Fix #30] Fix multithread race condition on global variable _factory #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 9 additions & 3 deletions langdetect/detector_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from .lang_detect_exception import ErrorCode, LangDetectException
from .utils.lang_profile import LangProfile

from threading import Lock


class DetectorFactory(object):
'''
Expand Down Expand Up @@ -116,12 +118,16 @@ def get_lang_list(self):

PROFILES_DIRECTORY = path.join(path.dirname(__file__), 'profiles')
_factory = None
factory_lock = Lock()


def init_factory():
global _factory
if _factory is None:
_factory = DetectorFactory()
_factory.load_profile(PROFILES_DIRECTORY)
with factory_lock:
if _factory is None:
_factory = DetectorFactory()
_factory.load_profile(PROFILES_DIRECTORY)


def detect(text):
init_factory()
Expand Down