Skip to content

Add no-GIL interpreter support #641

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

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
24 changes: 22 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
matrix:
os: ["ubuntu-latest", "windows-latest", "macos-latest"]
py: ["3.14-dev", "3.13", "3.12", "3.11", "3.10", "3.9"]
py: ["3.14-dev", "3.14t-dev", "3.13", "3.13t", "3.12", "3.11", "3.10", "3.9"]

runs-on: ${{ matrix.os }}
name: Run test with Python ${{ matrix.py }} on ${{ matrix.os }}
Expand All @@ -29,25 +29,45 @@ jobs:
- name: Prepare
shell: bash
run: |
pip install -U pip
# TODO: Workaround for Windows tests failing when upgrading `pip` with exit code 1
# pip install -U pip
pip install -r requirements.txt pytest

- name: Install pytest-run-parallel under free-threading
if: contains(matrix.py, 't')
run: |
pip install pytest-run-parallel

- name: Build
shell: bash
run: |
make cython
pip install .

- name: Test (C extension)
if: ! contains(matrix.py, 't')
shell: bash
run: |
pytest -v test

- name: Test (pure Python fallback)
if: ! contains(matrix.py, 't')
shell: bash
run: |
MSGPACK_PUREPYTHON=1 pytest -v test

- name: Test (C extension) in parallel under free-threading
if: contains(matrix.py, 't')
shell: bash
run: |
pytest -v --parallel-threads=auto --iterations=20 test

- name: Test (pure Python fallback) in parallel under free-threading
if: contains(matrix.py, 't')
shell: bash
run: |
MSGPACK_PUREPYTHON=1 pytest -v --parallel-threads=auto --iterations=20 test

- name: build packages
shell: bash
run: |
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/wheel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ jobs:
env:
CIBW_TEST_REQUIRES: "pytest"
CIBW_TEST_COMMAND: "pytest {package}/test"
CIBW_SKIP: "pp* cp38-*"
CIBW_SKIP: "pp* cp38-macosx_*"
CIBW_ENABLE: cpython-freethreading

- name: Build sdist
if: runner.os == 'Linux' && runner.arch == 'X64'
Expand Down
2 changes: 1 addition & 1 deletion msgpack/_cmsgpack.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding: utf-8
#cython: embedsignature=True, c_string_encoding=ascii, language_level=3
#cython: embedsignature=True, c_string_encoding=ascii, language_level=3, freethreading_compatible=True
from cpython.datetime cimport import_datetime, datetime_new
import_datetime()

Expand Down
2 changes: 1 addition & 1 deletion msgpack/_packer.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding: utf-8

# cython: freethreading_compatible = True
from cpython cimport *
from cpython.bytearray cimport PyByteArray_Check, PyByteArray_CheckExact
from cpython.datetime cimport (
Expand Down
2 changes: 1 addition & 1 deletion msgpack/_unpacker.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding: utf-8

# cython: freethreading_compatible = True
from cpython cimport *
cdef extern from "Python.h":
ctypedef struct PyObject
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Cython~=3.1.2
#pytest-run-parallel[psutil]
4 changes: 2 additions & 2 deletions test/test_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_unpack_bytearray():
obj = unpackb(buf, use_list=1)
assert [b"foo", b"bar"] == obj
expected_type = bytes
assert all(type(s) == expected_type for s in obj)
assert all(type(s) is expected_type for s in obj)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These also seem unrelated to the purpose of the PR (adding support for free-threading).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed lints as well in this PR



def test_unpack_memoryview():
Expand All @@ -26,7 +26,7 @@ def test_unpack_memoryview():
obj = unpackb(view, use_list=1)
assert [b"foo", b"bar"] == obj
expected_type = bytes
assert all(type(s) == expected_type for s in obj)
assert all(type(s) is expected_type for s in obj)


def test_packer_getbuffer():
Expand Down
51 changes: 51 additions & 0 deletions test/test_multithreading.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python3

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to #613 (comment), sharing Packer objects wouldn't be supported under free-threading, so this test is probably not needed. Could we run the test suite with pytest-run-parallel instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whatever works: I borrowed this test from another fork, and I'm still learning how to modernize existing pytest tests to use pytest-run-parallel

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I would do is install pytest-run-parallel in CI and then add a step in the main test job that looks something like this:

- name: Install pytest-run-parallel under free-threading
  if: matrix.py == '3.14t-dev' || matrix.py == '3.13t'
  shell: bash
  run: |
    pip install pytest-run-parallel

- name: Run tests in parallel under free-threading
  if: matrix.py == '3.14t-dev' || matrix.py == '3.13t'
  shell: bash
  run: |
    pytest -v --parallel-threads=auto --iterations=20 test

import threading
from concurrent.futures import ThreadPoolExecutor

from msgpack import Packer


def run_threaded(
func,
num_threads=8,
pass_count=False,
pass_barrier=False,
outer_iterations=1,
prepare_args=None,
):
"""Runs a function many times in parallel"""
for _ in range(outer_iterations):
with ThreadPoolExecutor(max_workers=num_threads) as tpe:
if prepare_args is None:
args = []
else:
args = prepare_args()
if pass_barrier:
barrier = threading.Barrier(num_threads)
args.append(barrier)
if pass_count:
all_args = [(func, i, *args) for i in range(num_threads)]
else:
all_args = [(func, *args) for i in range(num_threads)]
try:
futures = []
for arg in all_args:
futures.append(tpe.submit(*arg))
finally:
if len(futures) < num_threads and pass_barrier:
barrier.abort()
for f in futures:
f.result()


def test_multithread_packing():
output = []
test_data = "abcd" * 10_000_000
packer = Packer()

def closure(b):
data = packer.pack(test_data)
output.append(data)
b.wait()

run_threaded(closure, num_threads=10, pass_barrier=True, pass_count=False)
Loading