From c424b7abc8be90bdef1433c8acc2fa5e9b458d97 Mon Sep 17 00:00:00 2001 From: nggit <12218311+nggit@users.noreply.github.com> Date: Fri, 11 Oct 2024 19:44:34 +0700 Subject: [PATCH 1/3] simplify, explicit lock is not necessary in this design --- awaiter/__init__.py | 42 +++++++++++++++++------------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/awaiter/__init__.py b/awaiter/__init__.py index 3e1b298..f638dc3 100644 --- a/awaiter/__init__.py +++ b/awaiter/__init__.py @@ -8,7 +8,7 @@ from functools import wraps # noqa: E402 from inspect import isgeneratorfunction # noqa: E402 from queue import SimpleQueue # noqa: E402 -from threading import Thread, current_thread, Lock # noqa: E402 +from threading import Thread, current_thread # noqa: E402 def set_result(fut, result): @@ -127,14 +127,12 @@ def __init__(self, size=10, loop=None, name='MultiThreadExecutor'): self.size = size self._threads = {} - self._delete_lock = Lock() self._shutdown = None def is_alive(self): - with self._delete_lock: - for thread in self._threads.values(): - if thread.is_alive(): - return True + for thread in self._threads.values(): + if thread.is_alive(): + return True return False @@ -149,30 +147,24 @@ def run(self): super().run() self.size -= 1 finally: - with self._delete_lock: - if current_thread().name in self._threads: - del self._threads[current_thread().name] - - if self.is_alive(): - # exited normally. signal the next thread to stop as well - self.queue.put_nowait((None, None, None, None)) - else: self.loop.call_soon_threadsafe(current_thread().join) - self.loop.call_soon_threadsafe(set_result, self._shutdown, None) + + if current_thread().name in self._threads: + del self._threads[current_thread().name] + + # exited normally. signal the next thread to stop as well + self.loop.call_soon_threadsafe(self.shutdown) def submit(self, *args, **kwargs): fut = super().submit(*args, **kwargs) + num = len(self._threads) - with self._delete_lock: - num = len(self._threads) - - if num < self.size: - thread = Thread( - target=self.run, - name=f'{self.name}.{num}.{self.loop.time()}' - ) - thread.start() - self._threads[thread.name] = thread + if num < self.size: + thread = Thread( + target=self.run, name=f'{self.name}.{num}.{self.loop.time()}' + ) + thread.start() + self._threads[thread.name] = thread return fut From d92dc4dad69095ff133b05e9f96151117aaa7556 Mon Sep 17 00:00:00 2001 From: nggit <12218311+nggit@users.noreply.github.com> Date: Fri, 11 Oct 2024 19:57:59 +0700 Subject: [PATCH 2/3] clean up and fix ci --- ...nd-coverage.yml => tests_and_coverage.yml} | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) rename .github/workflows/{tests-and-coverage.yml => tests_and_coverage.yml} (80%) diff --git a/.github/workflows/tests-and-coverage.yml b/.github/workflows/tests_and_coverage.yml similarity index 80% rename from .github/workflows/tests-and-coverage.yml rename to .github/workflows/tests_and_coverage.yml index ce6b6f2..73f9658 100644 --- a/.github/workflows/tests-and-coverage.yml +++ b/.github/workflows/tests_and_coverage.yml @@ -1,17 +1,19 @@ name: Tests and Coverage + on: push: branches: ['testing'] pull_request: - branches: ['main', 'master', 'testing'] + branches: ['main', 'master'] workflow_dispatch: + jobs: tests: - name: Python ${{ matrix.python-version }} on ${{ matrix.os }} + name: Python ${{ matrix.python_version }} on ${{ matrix.os }} runs-on: ${{ matrix.os }} strategy: matrix: - python-version: + python_version: - '3.12' - '3.11' - '3.9' @@ -22,45 +24,54 @@ jobs: timeout-minutes: 10 steps: - uses: actions/checkout@v4 + - name: Setup Python uses: actions/setup-python@v4 with: - python-version: ${{ matrix.python-version }} + python-version: ${{ matrix.python_version }} + - name: Install dependencies run: | python -m pip install --upgrade pip python -m pip install --upgrade coverage + - name: Lint run: | python -m pip install --upgrade bandit python -m bandit --recursive awaiter python -m pip install --upgrade flake8 python -m flake8 . - if: ${{ matrix.os == 'ubuntu-20.04' && matrix.python-version == '3.12' }} + if: ${{ matrix.os == 'ubuntu-20.04' && matrix.python_version == '3.12' }} + - name: Run tests run: python tests.py - if: ${{ matrix.python-version != '3.12' }} + if: ${{ matrix.python_version != '3.12' }} + - name: Run tests with coverage run: | python -m coverage run tests.py mkdir artifact && mv .coverage artifact/.coverage.${{ matrix.os }} - if: ${{ matrix.python-version == '3.12' && !startsWith(matrix.os, 'windows-') }} + if: ${{ matrix.python_version == '3.12' && !startsWith(matrix.os, 'windows-') }} + - name: Run tests with coverage on Windows run: | python -m coverage run tests.py mkdir artifact && move .coverage artifact\.coverage.windows shell: cmd - if: ${{ matrix.python-version == '3.12' && startsWith(matrix.os, 'windows-') }} + if: ${{ matrix.python_version == '3.12' && startsWith(matrix.os, 'windows-') }} + - uses: actions/upload-artifact@v3 with: path: artifact - if: ${{ matrix.python-version == '3.12' }} + include-hidden-files: true + if: ${{ matrix.python_version == '3.12' }} report: name: Upload Coverage to Codecov & SonarCloud Scan needs: ['tests'] runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - name: Install dependencies run: | python -m venv --system-site-packages .local @@ -68,15 +79,18 @@ jobs: python -m pip install --upgrade pip python -m pip install --upgrade coverage - uses: actions/download-artifact@v3 + - name: Combine and view report run: | python -m coverage combine artifact python -m coverage report --show-missing --skip-covered python -m coverage xml + - uses: codecov/codecov-action@v4 with: token: ${{ secrets.CODECOV_TOKEN }} - - uses: sonarsource/sonarcloud-github-action@master + + - uses: sonarsource/sonarcloud-github-action@v3 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # needed to get PR information, if any SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} From ff261eef97f21ad29718f81222099f53c1d05365 Mon Sep 17 00:00:00 2001 From: nggit <12218311+nggit@users.noreply.github.com> Date: Fri, 11 Oct 2024 20:02:50 +0700 Subject: [PATCH 3/3] release 0.1.2 --- awaiter/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/awaiter/__init__.py b/awaiter/__init__.py index f638dc3..3c7a6b7 100644 --- a/awaiter/__init__.py +++ b/awaiter/__init__.py @@ -1,6 +1,6 @@ # Copyright (c) 2024 nggit -__version__ = '0.1.1' +__version__ = '0.1.2' __all__ = ('ThreadExecutor', 'MultiThreadExecutor') import asyncio # noqa: E402 @@ -122,7 +122,7 @@ def shutdown(self): class MultiThreadExecutor(ThreadExecutor): - def __init__(self, size=10, loop=None, name='MultiThreadExecutor'): + def __init__(self, size=5, loop=None, name='MultiThreadExecutor'): super().__init__(loop=loop, name=name) self.size = size