-
Notifications
You must be signed in to change notification settings - Fork 289
Ensure thread-safety for word_tokenize() wrapper functions #1213
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
96c6fbc
Initial plan
Copilot 783a66d
Add thread safety to longest and attacut tokenizers
Copilot b92f6af
Add thread-safety documentation to tokenizer docstrings
Copilot 6ac9d57
Add thread safety to pyicu (icu engine) tokenizer
Copilot dd587c6
Add thread safety documentation
Copilot 6755885
Add documentation about custom_dict thread safety and lazy loading
Copilot 1fe8e81
Apply code review suggestions: add attacut test, remove global declar…
Copilot 3e0600c
Rename thread_safety.md to threadsafe.md
bact c6d80de
Update copyright year in test_tokenize_thread_safety.py
bact 09b4e9b
Update thread safety notes in core.py
bact ae90a9a
Convert threadsafe.md to threadsafe.rst for Sphinx documentation
Copilot 0371325
Add thread safety to sefr_cut, oskut, wtsplit, and budoux engines
Copilot 2ac135f
Update threadsafe.rst
bact 75c4fcf
Update thread-safety notes for tokenizers
bact b325385
Update thread-safety documentation
bact f8e9a23
Update thread safety notes for stateless wrappers
bact d7f6a51
Clarify thread-safety disclaimers for external library wrappers
Copilot 8d7c80c
Add references to external library documentation for thread-safety
Copilot 71230c6
Fix documentation issues: add thread join, correct stress test claim,…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| Thread safety in PyThaiNLP word tokenization | ||
| ============================================== | ||
|
|
||
| Summary | ||
| ------- | ||
|
|
||
| PyThaiNLP's core word tokenization engines are designed with thread-safety | ||
| in mind. Internal implementations (``mm``, ``newmm``, ``newmm-safe``, | ||
| ``longest``, ``icu``) are thread-safe. | ||
|
|
||
| For engines that wrap external libraries (``attacut``, ``budoux``, ``deepcut``, | ||
| ``nercut``, ``nlpo3``, ``oskut``, ``sefr_cut``, ``tltk``, ``wtsplit``), the | ||
| wrapper code is thread-safe, but we cannot guarantee thread-safety of the | ||
| underlying external libraries themselves. | ||
|
|
||
| Thread safety implementation | ||
| ----------------------------- | ||
|
|
||
| **Internal implementations (fully thread-safe):** | ||
|
|
||
| - ``mm``, ``newmm``, ``newmm-safe``: Stateless implementation, | ||
| all data is local | ||
| - ``longest``: uses lock-protected check-then-act for | ||
| the management of global cache shared across threads | ||
| - ``icu``: each thread gets its own ``BreakIterator`` instance | ||
|
|
||
| **External library wrappers (wrapper code is thread-safe):** | ||
|
|
||
| - ``attacut``: uses lock-protected check-then-act for | ||
| the management of global cache; underlying library thread-safety not guaranteed | ||
| - ``budoux``: uses lock-protected lazy initialization of parser; | ||
| underlying library thread-safety not guaranteed | ||
| - ``deepcut``, ``nercut``, ``nlpo3``, ``tltk``: Stateless wrapper, | ||
| underlying library thread-safety not guaranteed | ||
| - ``oskut``, ``sefr_cut``, ``wtsplit``: use lock-protected model | ||
| loading when switching models/engines; underlying library thread-safety not guaranteed | ||
|
|
||
| Usage in multi-threaded applications | ||
| ------------------------------------- | ||
|
|
||
| Using a tokenization engine safely in multi-threaded contexts: | ||
|
|
||
| .. code-block:: python | ||
| import threading | ||
| from pythainlp.tokenize import word_tokenize | ||
| def tokenize_worker(text, results, index): | ||
| # Thread-safe for all engines | ||
| results[index] = word_tokenize(text, engine="longest") | ||
| texts = ["ผมรักประเทศไทย", "วันนี้อากาศดี", "เขาไปโรงเรียน"] | ||
| results = [None] * len(texts) | ||
| threads = [] | ||
| for i, text in enumerate(texts): | ||
| thread = threading.Thread(target=tokenize_worker, args=(text, results, i)) | ||
| threads.append(thread) | ||
| thread.start() | ||
| for thread in threads: | ||
| thread.join() | ||
| # All results are correctly populated | ||
| print(results) | ||
| Performance considerations | ||
| -------------------------- | ||
|
|
||
| 1. **Lock-based synchronization** (longest, attacut): | ||
|
|
||
| - Minimal overhead for cache access | ||
| - Cache lookups are very fast | ||
| - Lock contention is minimal in typical usage | ||
|
|
||
| 2. **Thread-local storage** (icu): | ||
|
|
||
| - Each thread maintains its own instance | ||
| - No synchronization overhead after initialization | ||
| - Slightly higher memory usage (one instance per thread) | ||
|
|
||
| 3. **Stateless engines** (newmm, mm): | ||
|
|
||
| - Zero synchronization overhead | ||
| - Best performance in multi-threaded scenarios | ||
| - Recommended for high-throughput applications | ||
|
|
||
| Best practices | ||
| -------------- | ||
|
|
||
| 1. **For high-throughput applications**: Consider using stateless engines like | ||
| ``newmm`` or ``mm`` for optimal performance. | ||
|
|
||
| 2. **For custom dictionaries**: The ``longest`` engine with custom dictionaries | ||
| maintains a cache per dictionary object. Reuse dictionary objects across | ||
| threads to maximize cache efficiency. | ||
|
|
||
| 3. **For process pools**: All engines work correctly with multiprocessing as | ||
| each process has its own memory space. | ||
|
|
||
| 4. **IMPORTANT: Do not modify custom dictionaries during tokenization**: | ||
|
|
||
| - Create your custom Trie/dictionary before starting threads | ||
| - Never call ``trie.add()`` or ``trie.remove()`` while tokenization is in progress | ||
| - If you need to update the dictionary, | ||
| create a new Trie instance and pass it to subsequent tokenization calls | ||
| - The Trie data structure itself is NOT thread-safe for concurrent modifications | ||
|
|
||
| Example of safe custom dictionary usage | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| .. code-block:: python | ||
| from pythainlp.tokenize import word_tokenize | ||
| from pythainlp.corpus.common import thai_words | ||
| from pythainlp.util import dict_trie | ||
| import threading | ||
| # SAFE: Create dictionary once before threading | ||
| custom_words = set(thai_words()) | ||
| custom_words.add("คำใหม่") | ||
| custom_dict = dict_trie(custom_words) | ||
| texts = ["ผมรักประเทศไทย", "วันนี้อากาศดี", "เขาไปโรงเรียน"] | ||
| def worker(text, custom_dict): | ||
| # SAFE: Only reading from the dictionary | ||
| return word_tokenize(text, engine="newmm", custom_dict=custom_dict) | ||
| # All threads share the same dictionary (read-only) | ||
| threads = [] | ||
| for text in texts: | ||
| t = threading.Thread(target=worker, args=(text, custom_dict)) | ||
| threads.append(t) | ||
| t.start() | ||
| # Wait for all threads to finish | ||
| for t in threads: | ||
| t.join() | ||
| Example of UNSAFE usage (DO NOT DO THIS) | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| .. code-block:: python | ||
| # UNSAFE: Modifying dictionary while threads are using it | ||
| custom_dict = dict_trie(thai_words()) | ||
| def unsafe_worker(text, custom_dict): | ||
| result = word_tokenize(text, engine="newmm", custom_dict=custom_dict) | ||
| # DANGER: Modifying the shared dictionary | ||
| custom_dict.add("คำใหม่") # This is NOT thread-safe! | ||
| return result | ||
| Testing | ||
| ------- | ||
|
|
||
| Comprehensive thread safety tests are available in: | ||
|
|
||
| - ``tests/core/test_tokenize_thread_safety.py`` | ||
|
|
||
| The test suite includes: | ||
|
|
||
| - Concurrent tokenization with multiple threads | ||
| - Race condition testing with multiple dictionaries | ||
| - Verification of result consistency across threads | ||
| - Stress testing with up to 200 concurrent operations (20 threads × 10 iterations) | ||
|
|
||
| Maintenance notes | ||
| ----------------- | ||
|
|
||
| When adding new tokenization engines to PyThaiNLP: | ||
|
|
||
| 1. **Avoid global mutable state** whenever possible | ||
| 2. If caching is necessary, use thread-safe locks | ||
| 3. If per-thread state is needed, use ``threading.local()`` | ||
| 4. Always add thread safety tests for new engines | ||
| 5. Document thread safety guarantees in docstrings | ||
|
|
||
| Related files | ||
| ------------- | ||
|
|
||
| - Core implementation: ``pythainlp/tokenize/core.py`` | ||
| - Engine implementations: ``pythainlp/tokenize/*.py`` | ||
| - Tests: ``tests/core/test_tokenize_thread_safety.py`` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The safe example is incomplete - threads are started but never joined, which means the example doesn't wait for completion. Add
for thread in threads: thread.join()after starting all threads to make this a complete, runnable example.