How do you stop numpy from multithreading? [duplicate] How do you stop numpy from multithreading? [duplicate] numpy numpy

How do you stop numpy from multithreading? [duplicate]


Only hopefully this fixes all scenarios and system you may be on.

  1. Use numpy.__config__.show() to see if you are using OpenBLAS or MKL

From this point on there are a few ways you can do this.

2.1. The terminal route export OPENBLAS_NUM_THREADS=1 or export MKL_NUM_THREADS=1

2.2 (This is my preferred way) In your python script import os and add the line os.environ['OPENBLAS_NUM_THREADS'] = '1' or os.environ['MKL_NUM_THREADS'] = '1'.

NOTE when setting os.environ[VAR] the number of threads must be a string! Also, you may need to set this environment variable before importing numpy/scipy.

There are probably other options besides openBLAS or MKL but step 1 will help you figure that out.


Set the MKL_NUM_THREADS environment variable to 1. As you might have guessed, this environment variable controls the behavior of the Math Kernel Library which is included as part of Enthought's numpy build.

I just do this in my startup file, .bash_profile, with export MKL_NUM_THREADS=1. You should also be able to do it from inside your script to have it be process specific.


In case you want to set the number of threads dynamically, and not globally via an environment variable, you can also do:

import mklmkl.set_num_threads(2)