Skip to content

Commit

Permalink
run script before running tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aktech committed Jul 18, 2024
1 parent 1c41148 commit 38a3367
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
31 changes: 28 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,54 @@ permissions:

jobs:
build:
strategy:
matrix:
version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
runs-on: ubuntu-latest
defaults:
run:
shell: bash -el {0}
steps:
- uses: actions/checkout@v3

- name: Set up Python
if: matrix.version != 3.13
uses: actions/setup-python@v4
with:
python-version: "${{ matrix.version }}"
allow-prereleases: true

- name: Setup Python via Miniconda
if: matrix.version == 3.13
uses: conda-incubator/setup-miniconda@v3.0.4
with:
python-version: 3.13
channels: defaults,ad-testing/label/py313_nogil

- name: Check Python version
run: |
python -VV
python -c "import sys; print(sys._is_gil_enabled())"
- name: Check Python GIL enabled
if: matrix.version == 3.13
run: python -c "import sys; print(sys._is_gil_enabled())"

- name: Install dependencies
run: |
pip install setuptools
- name: Build and run tests
- name: Build C extension
run: |
python python_c_extension/setup.py build
cp -r build/*/* python_c_extension
python -m unittest discover tests/ -vvv
- name: Run script in single and multi-thread mode
run: |
python python_c_extension/c_extension_gil.py
env:
PYTHON_GIL: 0

- name: Run Tests
run: python -m unittest discover tests/ -vvv
env:
PYTHON_GIL: 0
21 changes: 14 additions & 7 deletions python_c_extension/c_extension_gil.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,31 @@

N = int(10e8)

def count_pure_python(n):
while n > 0:
n -= 1

def run_count_in_single_thread():

def run_count_in_single_thread(count_func):
t1 = time()
count(N)
count_func(N)
time_taken = time() - t1
return time_taken


def run_count_in_multiple_threads():
def run_count_in_multiple_threads(count_func):
t1 = time()
th1 = Thread(target=count, args=(N // 2,))
th2 = Thread(target=count, args=(N // 2,))
th1 = Thread(target=count_func, args=(N // 2,))
th2 = Thread(target=count_func, args=(N // 2,))
th1.start(); th2.start()
th1.join(); th2.join()
time_taken = time() - t1
return time_taken


if __name__ == '__main__':
print('Single Thread: %8f' % (run_count_in_single_thread()))
print('Two Threads: %8f' % (run_count_in_multiple_threads()))
print('Single Thread (C extension): %8f' % (run_count_in_single_thread(count)))
print('Two Threads (C extension): %8f' % (run_count_in_multiple_threads(count)))

print('Single Thread (Pure Python): %8f' % (run_count_in_single_thread(count_pure_python)))
print('Two Threads (Pure Python): %8f' % (run_count_in_multiple_threads(count_pure_python)))

0 comments on commit 38a3367

Please sign in to comment.