blosc2.set_nthreads

blosc2.set_nthreads(nthreads: int) → int[source]

Set the number of threads to be used during Blosc operations.

Parameters:

nthreads (int) – The number of threads to be used during Blosc operations.

Returns:

out – The previous number of threads used.

Return type:

int

Raises:

ValueError – If nthreads is larger than the maximum number of threads Blosc can use. If nthreads is not a positive integer.

Notes

The number of threads can also be set via the BLOSC_NTHREADS environment variable (e.g., export BLOSC_NTHREADS=1), which nthreads then starts from. While it is set, C-Blosc2 uses it for every compression and decompression, over this function and the nthreads of CParams and DParams. Expression execution uses native miniexpr scheduling or explicit NumPy fallbacks; NumExpr thread settings do not affect Blosc2.

The maximum number of threads for Blosc is \(2^{31} - 1\). In some cases, Blosc gets better results if you set the number of threads to a value slightly below your number of cores (via detect_number_of_cores()).

Examples

Set the number of threads to 2 and then to 1:

>>> oldn = blosc2.set_nthreads(2)
>>> blosc2.set_nthreads(1)
2
>>> _ = blosc2.set_nthreads(oldn)

See also

nthreads