Cython uses OpenMP for it's multithreading capabilities.
To enable OpenMP the compiler will need an extra flag to be passed to it while compiling and linking otherwise the parallel part of your code will be ignored.
The flags for some popular compilers are as follows:
- GCC = -fopenmp
- MSVC = /openmp
- icc = -openmp
Assuming that you have saved your function in the file test.pyx
the following setup.py
should work if you are using GCC.
from distutils.core import setup, Extension
from Cython.Build import cythonize
extensions = [Extension(
"test",
sources=["test.pyx"],
extra_compile_args=["-fopenmp"],
extra_link_args=["-fopenmp"]
)]
setup(
ext_modules = cythonize(extensions)
)
Once compiled like that the code should spawn 10 threads when run:
In [1]: import test
In [2]: test.test_func()
Thread ID: 9
Thread ID: 1
Thread ID: 6
Thread ID: 7
Thread ID: 3
Thread ID: 8
Thread ID: 5
Thread ID: 4
Thread ID: 0
Thread ID: 2
If you want some more information than this page in the cython docs has a good basic guide of how to use parallelism with cython.