From 29e2fdec64fa48f075915542f6154ba768006156 Mon Sep 17 00:00:00 2001 From: Timur Osmanov <54434686+TOsmanov@users.noreply.github.com> Date: Tue, 23 Sep 2025 09:51:14 +0300 Subject: [PATCH 1/5] add semaphore for run_in_thread --- .../preprocessors/utils/preprocessor_ext.py | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/foliant/preprocessors/utils/preprocessor_ext.py b/foliant/preprocessors/utils/preprocessor_ext.py index d3cf5f5..db2cace 100644 --- a/foliant/preprocessors/utils/preprocessor_ext.py +++ b/foliant/preprocessors/utils/preprocessor_ext.py @@ -1,23 +1,32 @@ +import os import re import traceback import threading from pathlib import Path -from typing import Callable -from typing import Optional -from typing import Union +from typing import Callable, Optional, Union +from functools import wraps from foliant.preprocessors.base import BasePreprocessor from foliant.utils import output +MAX_THREADS = max(1, os.cpu_count() - 2) +thread_semaphore = threading.Semaphore(MAX_THREADS) + def run_in_thread(fn): - def run(*k, **kw): - t = threading.Thread(target=fn, args=k, kwargs=kw) - t.start() - return t - return run + @wraps(fn) + def run(*args, **kwargs): + def wrapper(): + try: + fn(*args, **kwargs) + finally: + thread_semaphore.release() + + thread_semaphore.acquire() + threading.Thread(target=wrapper, daemon=True).start() + return run def allow_fail(msg: str = 'Failed to process tag. Skipping.') -> Callable: """ From 12d8b4aef2b4dd2b96e2273e70ec4ea08cc2596b Mon Sep 17 00:00:00 2001 From: Timur Osmanov <54434686+TOsmanov@users.noreply.github.com> Date: Tue, 23 Sep 2025 10:02:33 +0300 Subject: [PATCH 2/5] Update python-test.yml --- .github/workflows/python-test.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml index 9cd2e56..43783fc 100644 --- a/.github/workflows/python-test.yml +++ b/.github/workflows/python-test.yml @@ -3,8 +3,7 @@ name: Python package on: [push] jobs: - build: - + test: runs-on: ubuntu-latest strategy: matrix: From a859cbc030c11e8f3dfc96ad89bdd28fdeb2f590 Mon Sep 17 00:00:00 2001 From: Timur Osmanov <54434686+TOsmanov@users.noreply.github.com> Date: Tue, 23 Sep 2025 10:04:38 +0300 Subject: [PATCH 3/5] Update setup.py --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 07b3c74..e83aa31 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ description=SHORT_DESCRIPTION, long_description=LONG_DESCRIPTION, long_description_content_type='text/markdown', - version='1.0.4', + version='1.0.5', author='Daniil Minukhin', author_email='ddddsa@gmail.com', url='https://github.com/foliant-docs/foliantcontrib.utils', From aa735ba388ee81723033b4dd8f1f8e1bece09607 Mon Sep 17 00:00:00 2001 From: Timur Osmanov <54434686+TOsmanov@users.noreply.github.com> Date: Tue, 23 Sep 2025 12:39:35 +0300 Subject: [PATCH 4/5] Update changelog.md --- changelog.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/changelog.md b/changelog.md index 903260a..411437a 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,7 @@ +# 1.0.5 + +- PreprocessorExt: fix decorator `run_in_thread` + # 1.0.4 - utils: add `move_files_threadpool` function From 8db21e005c4a8893cce45e2dbd3cefdb4c6f2c84 Mon Sep 17 00:00:00 2001 From: Timur Osmanov <54434686+TOsmanov@users.noreply.github.com> Date: Tue, 23 Sep 2025 12:41:41 +0300 Subject: [PATCH 5/5] Update changelog.md --- changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 411437a..903038b 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,6 @@ # 1.0.5 -- PreprocessorExt: fix decorator `run_in_thread` +- PreprocessorExt: fixed the definition of `run_in_thread`, which is a decorator for the file processing function. # 1.0.4