Skip to content

Commit ce84452

Browse files
committed
Support free-threaded Python and release the GIL around native work
1 parent 8f5cadc commit ce84452

21 files changed

Lines changed: 175800 additions & 59843 deletions

‎.github/workflows/tests-dev.yml‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
runs-on: ubuntu-latest
1717
strategy:
1818
matrix:
19-
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13", "3.14"]
19+
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13", "3.14", "3.14t", "3.15t"]
2020
env:
2121
TA_INCLUDE_PATH: ${{ github.workspace }}/ta-lib-c/include
2222
TA_LIBRARY_PATH: ${{ github.workspace }}/ta-lib-c/lib
@@ -56,15 +56,20 @@ jobs:
5656
cmake --install ta-lib-c-build
5757
5858
- name: Set up Python ${{ matrix.python-version }}
59-
uses: actions/setup-python@v4
59+
uses: actions/setup-python@v6
6060
with:
6161
python-version: ${{ matrix.python-version }}
6262
allow-prereleases: true
6363

6464
- name: Install dependencies
6565
run: |
6666
pip install --upgrade pip wheel setuptools
67-
pip install -r requirements_test.txt
67+
if [[ "${{ matrix.python-version }}" == *t ]]; then
68+
# Polars has no free-threaded wheels; exercise NumPy and pandas here.
69+
pip install -r requirements.txt pytest pandas
70+
else
71+
pip install -r requirements_test.txt
72+
fi
6873
pip install flake8
6974
7075
- name: Build cython modules in-place

‎.github/workflows/tests.yml‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
runs-on: ubuntu-latest
1414
strategy:
1515
matrix:
16-
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13", "3.14"]
16+
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13", "3.14", "3.14t", "3.15t"]
1717
steps:
1818
- uses: actions/checkout@v3
1919

@@ -31,15 +31,20 @@ jobs:
3131
version: ${{ steps.talib_c.outputs.version }}
3232

3333
- name: Set up Python ${{ matrix.python-version }}
34-
uses: actions/setup-python@v4
34+
uses: actions/setup-python@v6
3535
with:
3636
python-version: ${{ matrix.python-version }}
3737
allow-prereleases: true
3838

3939
- name: Install dependencies
4040
run: |
4141
pip install --upgrade pip wheel setuptools
42-
pip install -r requirements_test.txt
42+
if [[ "${{ matrix.python-version }}" == *t ]]; then
43+
# Polars has no free-threaded wheels; exercise NumPy and pandas here.
44+
pip install -r requirements.txt pytest pandas
45+
else
46+
pip install -r requirements_test.txt
47+
fi
4348
pip install flake8
4449
4550
- name: Build cython modules in-place

‎.github/workflows/wheels.yml‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ env:
1111
PIP_NO_VERIFY: 0
1212
PIP_VERBOSE: 1
1313
CIBW_BEFORE_BUILD: pip install -U setuptools Cython wheel meson-python ninja && pip install -U numpy
14-
CIBW_TEST_REQUIRES: pytest pandas maturin polars
15-
CIBW_SKIP: "pp* cp313t* cp314t*"
14+
CIBW_SKIP: "pp* cp313t*"
1615

1716
jobs:
1817
build_linux_x86_64:

‎README.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,31 @@ s.advance() # count a skipped bar: the range moves, the va
611611
A handle points into the TA-Lib C library, so it cannot be pickled or shared
612612
with another process. Keep the history and re-open instead.
613613
614+
## Threads
615+
616+
TA-Lib supports both regular and free-threaded CPython. Native calculations,
617+
including stream opening, updates, reads and cloning, release the GIL on
618+
regular CPython. Python argument conversion, array allocation and result
619+
construction still require Python thread state. Independent calculations run
620+
concurrently; operations on the same stream handle use a per-handle mutex.
621+
622+
Inputs must remain unchanged during a call: do not write to or resize an input
623+
array from another thread. Calls allocate their own output arrays.
624+
625+
Settings are process-wide. Set unstable periods, compatibility and candle
626+
settings before starting calculations or opening streams. Changing settings
627+
while a calculation is active or any stream is alive raises `RuntimeError`.
628+
After calls finish and all streams (including copies) are released, settings
629+
can be changed again. The settings gate protects lookback calculation, output
630+
allocation and computation together, while allowing simultaneous readers.
631+
632+
The synchronization covers calls through this Python extension; other code
633+
calling the same C library directly must observe TA-Lib's settings and lifecycle
634+
contract too. Separate subinterpreters are not supported.
635+
636+
Free-threaded Python requires a matching free-threaded extension build and
637+
compatible dependencies. Importing TA-Lib does not enable the GIL in that build.
638+
614639
## Supported Indicators and Functions 📋
615640
616641
We can show all the TA functions supported by TA-Lib, either as a `list` or

‎pyproject.toml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,10 @@ requires-python = '>=3.9'
4141
[tool.setuptools]
4242
packages = ["talib"]
4343
package-data = {"talib" = ["_ta_lib.pyi", "py.typed", "abstract.pyi", "stream.pyi"]}
44+
45+
[tool.cibuildwheel]
46+
test-requires = ["pytest", "pandas", "maturin", "polars"]
47+
48+
[[tool.cibuildwheel.overrides]]
49+
select = "cp3{14,15}t-*"
50+
test-requires = ["pytest", "pandas"]

‎setup.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import os.path
66
import warnings
7+
import sysconfig
78

89
from setuptools import setup, Extension
910

@@ -96,6 +97,8 @@ def build_extensions(self):
9697
Extension(
9798
'talib._ta_lib',
9899
['talib/_ta_lib.pyx' if has_cython else 'talib/_ta_lib.c'],
100+
define_macros=[('Py_GIL_DISABLED', '1')]
101+
if sys.platform == 'win32' and sysconfig.get_config_var('Py_GIL_DISABLED') else [],
99102
include_dirs=include_dirs,
100103
library_dirs=library_dirs,
101104
libraries=[lib_talib_name],

‎talib/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def wrapper(*args, **kwds):
148148
# the underlying TA-Lib.
149149

150150
_ta_initialize()
151-
atexit.register(_ta_shutdown)
151+
atexit.register(_ta_shutdown, True)
152152

153153
__function_groups__ = {
154154
'Cycle Indicators': [

‎talib/_abstract.pxi‎

Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,10 @@ class Function(object):
349349
elif type_ == lib.TA_OptInput_IntegerRange or type_ == lib.TA_OptInput_IntegerList:
350350
__ta_setOptInputParamInteger(holder, i, value)
351351

352-
lookback = __ta_getLookback(holder)
353-
__ta_paramHolderFree(holder)
352+
try:
353+
lookback = __ta_getLookback(holder)
354+
finally:
355+
__ta_paramHolderFree(holder)
354356
return lookback
355357

356358
@property
@@ -566,23 +568,39 @@ def _ta_getGroupTable():
566568
Returns the list of available TALIB function group names. *slow*
567569
"""
568570
cdef lib.TA_StringTable *table
569-
_ta_check_success('TA_GroupTableAlloc', lib.TA_GroupTableAlloc(&table))
570-
groups = []
571-
for i in xrange(table.size):
572-
groups.append(deref(&table.string[i]))
573-
_ta_check_success('TA_GroupTableFree', lib.TA_GroupTableFree(table))
571+
cdef TA_RetCode retCode
572+
with _TA_ReadLease():
573+
with nogil:
574+
retCode = lib.TA_GroupTableAlloc(&table)
575+
_ta_check_success('TA_GroupTableAlloc', retCode)
576+
try:
577+
groups = []
578+
for i in xrange(table.size):
579+
groups.append(deref(&table.string[i]))
580+
finally:
581+
with nogil:
582+
retCode = lib.TA_GroupTableFree(table)
583+
_ta_check_success('TA_GroupTableFree', retCode)
574584
return groups
575585

576586
def _ta_getFuncTable(char *group):
577587
"""
578588
Returns a list of the functions for the specified group name. *slow*
579589
"""
580590
cdef lib.TA_StringTable *table
581-
_ta_check_success('TA_FuncTableAlloc', lib.TA_FuncTableAlloc(group, &table))
582-
functions = []
583-
for i in xrange(table.size):
584-
functions.append(deref(&table.string[i]))
585-
_ta_check_success('TA_FuncTableFree', lib.TA_FuncTableFree(table))
591+
cdef TA_RetCode retCode
592+
with _TA_ReadLease():
593+
with nogil:
594+
retCode = lib.TA_FuncTableAlloc(group, &table)
595+
_ta_check_success('TA_FuncTableAlloc', retCode)
596+
try:
597+
functions = []
598+
for i in xrange(table.size):
599+
functions.append(deref(&table.string[i]))
600+
finally:
601+
with nogil:
602+
retCode = lib.TA_FuncTableFree(table)
603+
_ta_check_success('TA_FuncTableFree', retCode)
586604
return functions
587605

588606
def __get_flags(int flag, dict flags_lookup_dict):
@@ -644,7 +662,11 @@ def _ta_getFuncInfo(char *function_name):
644662
group, help, flags, num_inputs, num_opt_inputs and num_outputs.
645663
"""
646664
cdef const lib.TA_FuncInfo *info
647-
retCode = lib.TA_GetFuncInfo(__ta_getFuncHandle(function_name), &info)
665+
cdef const lib.TA_FuncHandle *handle = __ta_getFuncHandle(function_name)
666+
cdef TA_RetCode retCode
667+
with _TA_ReadLease():
668+
with nogil:
669+
retCode = lib.TA_GetFuncInfo(handle, &info)
648670
_ta_check_success('TA_GetFuncInfo', retCode)
649671

650672
return {
@@ -663,7 +685,11 @@ def _ta_getInputParameterInfo(char *function_name, int idx):
663685
keys: name and flags.
664686
"""
665687
cdef const lib.TA_InputParameterInfo *info
666-
retCode = lib.TA_GetInputParameterInfo(__ta_getFuncHandle(function_name), idx, &info)
688+
cdef const lib.TA_FuncHandle *handle = __ta_getFuncHandle(function_name)
689+
cdef TA_RetCode retCode
690+
with _TA_ReadLease():
691+
with nogil:
692+
retCode = lib.TA_GetInputParameterInfo(handle, idx, &info)
667693
_ta_check_success('TA_GetInputParameterInfo', retCode)
668694

669695
name = bytes2str(info.paramName)
@@ -684,7 +710,11 @@ def _ta_getOptInputParameterInfo(char *function_name, int idx):
684710
following keys: name, display_name, type, help, default_value and value.
685711
"""
686712
cdef const lib.TA_OptInputParameterInfo *info
687-
retCode = lib.TA_GetOptInputParameterInfo(__ta_getFuncHandle(function_name), idx, &info)
713+
cdef const lib.TA_FuncHandle *handle = __ta_getFuncHandle(function_name)
714+
cdef TA_RetCode retCode
715+
with _TA_ReadLease():
716+
with nogil:
717+
retCode = lib.TA_GetOptInputParameterInfo(handle, idx, &info)
688718
_ta_check_success('TA_GetOptInputParameterInfo', retCode)
689719

690720
name = bytes2str(info.paramName)
@@ -706,7 +736,11 @@ def _ta_getOutputParameterInfo(char *function_name, int idx):
706736
keys: name and flags.
707737
"""
708738
cdef const lib.TA_OutputParameterInfo *info
709-
retCode = lib.TA_GetOutputParameterInfo(__ta_getFuncHandle(function_name), idx, &info)
739+
cdef const lib.TA_FuncHandle *handle = __ta_getFuncHandle(function_name)
740+
cdef TA_RetCode retCode
741+
with _TA_ReadLease():
742+
with nogil:
743+
retCode = lib.TA_GetOutputParameterInfo(handle, idx, &info)
710744
_ta_check_success('TA_GetOutputParameterInfo', retCode)
711745

712746
name = bytes2str(info.paramName)
@@ -778,15 +812,23 @@ cdef const lib.TA_FuncHandle* __ta_getFuncHandle(char *function_name):
778812
Returns a pointer to a function handle for the given function name
779813
"""
780814
cdef const lib.TA_FuncHandle *handle
781-
_ta_check_success('TA_GetFuncHandle', lib.TA_GetFuncHandle(function_name, &handle))
815+
cdef TA_RetCode retCode
816+
with _TA_ReadLease():
817+
with nogil:
818+
retCode = lib.TA_GetFuncHandle(function_name, &handle)
819+
_ta_check_success('TA_GetFuncHandle', retCode)
782820
return handle
783821

784822
cdef lib.TA_ParamHolder* __ta_paramHolderAlloc(char *function_name):
785823
"""
786824
Returns a pointer to a parameter holder for the given function name
787825
"""
788826
cdef lib.TA_ParamHolder *holder
789-
retCode = lib.TA_ParamHolderAlloc(__ta_getFuncHandle(function_name), &holder)
827+
cdef const lib.TA_FuncHandle *handle = __ta_getFuncHandle(function_name)
828+
cdef TA_RetCode retCode
829+
with _TA_ReadLease():
830+
with nogil:
831+
retCode = lib.TA_ParamHolderAlloc(handle, &holder)
790832
_ta_check_success('TA_ParamHolderAlloc', retCode)
791833
return holder
792834

@@ -795,18 +837,28 @@ cdef int __ta_paramHolderFree(lib.TA_ParamHolder *params):
795837
Frees the memory allocated by __ta_paramHolderAlloc (call when done with the parameter holder)
796838
WARNING: Not properly calling this function will cause memory leaks!
797839
"""
798-
_ta_check_success('TA_ParamHolderFree', lib.TA_ParamHolderFree(params))
840+
cdef TA_RetCode retCode
841+
with nogil:
842+
retCode = lib.TA_ParamHolderFree(params)
843+
_ta_check_success('TA_ParamHolderFree', retCode)
799844

800845
cdef int __ta_setOptInputParamInteger(lib.TA_ParamHolder *holder, int idx, int value):
801-
retCode = lib.TA_SetOptInputParamInteger(holder, idx, value)
846+
cdef TA_RetCode retCode
847+
with nogil:
848+
retCode = lib.TA_SetOptInputParamInteger(holder, idx, value)
802849
_ta_check_success('TA_SetOptInputParamInteger', retCode)
803850

804851
cdef int __ta_setOptInputParamReal(lib.TA_ParamHolder *holder, int idx, double value):
805-
retCode = lib.TA_SetOptInputParamReal(holder, idx, value)
852+
cdef TA_RetCode retCode
853+
with nogil:
854+
retCode = lib.TA_SetOptInputParamReal(holder, idx, value)
806855
_ta_check_success('TA_SetOptInputParamReal', retCode)
807856

808857
cdef int __ta_getLookback(lib.TA_ParamHolder *holder):
809858
cdef int lookback
810-
retCode = lib.TA_GetLookback(holder, &lookback)
859+
cdef TA_RetCode retCode
860+
with _TA_ReadLease():
861+
with nogil:
862+
retCode = lib.TA_GetLookback(holder, &lookback)
811863
_ta_check_success('TA_GetLookback', retCode)
812864
return lookback

0 commit comments

Comments
 (0)