title: How to limit number of CPU's used by a python script w/o terminal or multiprocessing library?
body: <p>My main problem is issued <a href="https://stackoverflow.com/questions/55531880/does-partial-fit-runs-in-parallel-in-sklearn-decomposition-incrementalpca">here</a>. Since no one has given a solution yet, I have decided to find a workaround. I am looking for a way to limit a python scripts CPU usage (<strong>not priority but the number of CPU cores</strong>) with python code. I know I can do that with multiprocessing library (pool, etc.) but I am not the one who is running it with multiprocessing. So, I don't know how to that. And also I could do that via terminal but this script is being imported by another script. Unfortunately, I don't have the luxury of calling it through terminal.</p>
  <p>tl;dr: How to limit CPU usage (<strong>number of cores</strong>) of a python script, which is being imported by another script and I don't even know why it runs in parallel, without running it via terminal. Please check the code snippet below.</p>
  <p>The code snippet causing the issue:</p>
  <pre><code>from sklearn.datasets import load_digits
  from sklearn.decomposition import IncrementalPCA
  import numpy as np
  
  X, _ = load_digits(return_X_y=True)
  
  #Copy-paste and increase the size of the dataset to see the behavior at htop.
  for _ in range(8):
      X = np.vstack((X, X))
  
  print(X.shape)
  
  transformer = IncrementalPCA(n_components=7, batch_size=200)
  
  #PARTIAL FIT RUNS IN PARALLEL! GOD WHY?
  ---------------------------------------
  transformer.partial_fit(X[:100, :])
  ---------------------------------------
  X_transformed = transformer.fit_transform(X)
  
  print(X_transformed.shape)
  </code></pre>
  <p>Versions:</p>
  <ul>
  <li>Python 3.6</li>
  <li>joblib 0.13.2</li>
  <li>scikit-learn 0.20.2</li>
  <li>numpy 1.16.2</li>
  </ul>
  <p><strong>UPDATE: Doesn't work. Thank you for clarification @Darkonaut . The sad thing is, I already knew this wouldn't work and I already clearly stated on the question title but people don't read I guess.</strong>
  I guess I am doing it wrong. I've updated the code snippet based on the @Ben Chaliah Ayoub <a href="https://stackoverflow.com/a/55747038/10183880">answer</a>. Nothing seems to be changed. And also I want to point out to something: I am not trying to run this code on multiple cores. This line <code>transformer.partial_fit(X[:100, :])</code> running on multiple cores (for some reason) and it doesn't have <code>n_jobs</code> or anything. Also please note that my first example and my original code is not initialized with a pool or something similar. I can't set the number of cores in the first place (Because there is no such place). But now there is a place for it but it is still running on multiple cores. Feel free to test it yourself. (Code below) That's why I am looking for a workaround.</p>
  <pre><code>from sklearn.datasets import load_digits
  from sklearn.decomposition import IncrementalPCA
  import numpy as np
  from multiprocessing import Pool, cpu_count
  def run_this():
      X, _ = load_digits(return_X_y=True)
      #Copy-paste and increase the size of the dataset to see the behavior at htop.
      for _ in range(8):
          X = np.vstack((X, X))
      print(X.shape)
      #This is the exact same example taken from sckitlearn's IncrementalPCA website.
      transformer = IncrementalPCA(n_components=7, batch_size=200)
      transformer.partial_fit(X[:100, :])
      X_transformed = transformer.fit_transform(X)
      print(X_transformed.shape)
  pool= Pool(processes=1)
  pool.apply(run_this)
  </code></pre>
  <p><strong>UPDATE:</strong> So, I have tried to set blas threads using <a href="https://stackoverflow.com/a/53224849/10183880">this</a> in my code before importing numpy but it didn't work (again). Any other suggestions? The latest stage of code can be found below.</p>
  <p><em>Credits:</em> @Amir</p>
  <pre><code>from sklearn.datasets import load_digits
  from sklearn.decomposition import IncrementalPCA
  import os
  os.environ[&quot;OMP_NUM_THREADS&quot;] = &quot;1&quot; # export OMP_NUM_THREADS=1
  os.environ[&quot;OPENBLAS_NUM_THREADS&quot;] = &quot;1&quot; # export OPENBLAS_NUM_THREADS=1
  os.environ[&quot;MKL_NUM_THREADS&quot;] = &quot;1&quot; # export MKL_NUM_THREADS=1
  os.environ[&quot;VECLIB_MAXIMUM_THREADS&quot;] = &quot;1&quot; # export VECLIB_MAXIMUM_THREADS=1
  os.environ[&quot;NUMEXPR_NUM_THREADS&quot;] = &quot;1&quot; # export NUMEXPR_NUM_THREADS=1
  
  import numpy as np
  
  X, _ = load_digits(return_X_y=True)
  
  #Copy-paste and increase the size of the dataset to see the behavior at htop.
  for _ in range(8):
      X = np.vstack((X, X))
  
  print(X.shape)
  transformer = IncrementalPCA(n_components=7, batch_size=200)
  
  transformer.partial_fit(X[:100, :])
  
  X_transformed = transformer.fit_transform(X)
  
  print(X_transformed.shape)
  </code></pre>