How to set the max thread a python script could use when calling from shell
<p>Have script a.py, it will run some task with multiple-thread, please noticed that I have no control over the a.py.</p>

<p>I'm looking for a way to limit the number of thread it could use, as I found that using more threads than my CPU core will slow down the script.</p>

<p>It could be something like:</p>

<p>python  --nthread=2 a.py </p>

<p>or Modifying something in my OS is also acceptable . </p>

<p>I am using ubuntu 16.04</p>

<p>As requested: </p>

<p>the a.py is just a module in scikit-learn the <a href="https://github.com/scikit-learn/scikit-learn/blob/183722475189b8e21ee2e621b19acca3c9e054d8/sklearn/neural_network/multilayer_perceptron.py" rel="nofollow">MLPRegressor</a> .
I also asked this question <a href="https://github.com/scikit-learn/scikit-learn/issues/7280" rel="nofollow">here</a>.</p>

<p>A more general way, not specific to python: </p>

<pre><code>taskset -c 1-3 python yourProgram.py
</code></pre>

<p>In this case the threads 1-3 (3 in total) will be used. Any parallelization invoked by your program will share those resources. </p>

<p>For a solution that fits your exact problem you should better identify which part of the code parellelizes. For instance, if it is due to numpy routines, you could limit it by using: </p>

<pre><code>OMP_NUM_THREADS=4 python yourProgram.py
</code></pre>

<p>Again, the first solution is general and handled by the os, whereas the second is python (numpy) specific. </p>

python multithreading scikit-learn