SciPy - get_workers() Function



The get_workers() method is used to determine the number of workers that SciPy uses for parallel computations such as FFT (Fourier Transforms). It allows the user to check how many workers the set_workers() method has set.

This functionality is available in SciPy version 1.11.0 and later.

Syntax

Following is the syntax of the SciPy get_workers() method −

.get_workers()

Parameters

This method doesn'ttakeanyarguments.Thisisansimplemethodthatreturns the number of parallel workerscurrently assigned to computingtasks by set_workers() method.

Return Value

It returns the number of workers(threads) currently set for SciPy's parallel computations. It return's positive value (exact number of workers), negative value when adjusted based on total available CPU cores and default value when system-determined worker count if not explicitly set.

Example 1

This example shows how to check the number of CPU cores currently allocated for parallel computations using the get_workers() method.

from scipy.fft import set_workers, get_workers

set_workers(-1)
workers = get_workers()
print("Number of workers:", workers)

Following is an output of the above code −

Number of workers: 1

Example 2

This example shows how to use get_workers() to track changes in CPU core allocation when modifying parallel workers using set_workers(). It ensures optimal usage of resources and helps in the computation of task performance optimization.

from scipy.fft import set_workers, get_workers

default_workers = get_workers()
print("Default number of workers:", default_workers)

set_workers(2)
current_workers = get_workers()
print("Number of workers after update:", current_workers)

set_workers(-1)
updated_workers = get_workers()
print("Number of workers after using all but one core:", updated_workers)

Output of the above code is as follows −

Default number of workers: 1
Number of workers after update: 1
Number of workers after using all but one core: 1
scipy_discrete_fourier_transform.htm
Advertisements