# Limit Number of threads in python SimpleITK programatically

**URL:** <https://discourse.itk.org/t/limit-number-of-threads-in-python-simpleitk-programatically/2723>\
**Category:** Beginner Questions\
**Created:** [February 14, 2020, 8:19am UTC](https://discourse.itk.org/t/limit-number-of-threads-in-python-simpleitk-programatically/2723 "2020-02-14T08:19:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![reox](https://discourse.itk.org/user_avatar/discourse.itk.org/reox/32/4490_2.png) [@reox](https://discourse.itk.org/u/reox)\
**Post date:** [February 14, 2020, 8:19am UTC](https://discourse.itk.org/t/limit-number-of-threads-in-python-simpleitk-programatically/2723/1 "2020-02-14T08:19:09Z")

</div>

I’m using SimpleITK from python. By default all logical CPUs which are found are used, but I like to constrict this. I already read this thread here: [Threading in ITK](https://discourse.itk.org/t/threading-in-itk/782) and can confirm that setting the environment variable `ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS` does work.  
However, I would like to be able to set this from the program itself, so I don’t have to mess with the variables all the time.

I found out it is possible to set this inside the python program like:

```
import os
import psutil
os.environ['ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS'] = str(psutil.cpu_count(logical=False))
import SimpleITK as sitk

```

But this has to be before importing SimpleITK.  
Is it possible to set this also after SimpleITK was imported? I tried `importlib.reload(sitk)`, but this seems to change nothing, the old number of Threads is still used.

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [February 14, 2020, 2:11pm UTC](https://discourse.itk.org/t/limit-number-of-threads-in-python-simpleitk-programatically/2723/2 "2020-02-14T14:11:54Z")

</div>

Hello @reox,

The SimpleITK process object has a method [SetGlobalDefaultNumberOfThreads](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1ProcessObject.html#a305b43330f9feb26325eadfc30695bd9):

```auto
import SimpleITK as sitk
MAX_THREADS = 3

sitk.ProcessObject.SetGlobalDefaultNumberOfThreads(MAX_THREADS)

```

There’s the matching Get method so that you can find out what is the global default number of threads.

---

<div class="post-metadata">

**Author:** ![reox](https://discourse.itk.org/user_avatar/discourse.itk.org/reox/32/4490_2.png) [@reox](https://discourse.itk.org/u/reox)\
**Post date:** [February 17, 2020, 10:37am UTC](https://discourse.itk.org/t/limit-number-of-threads-in-python-simpleitk-programatically/2723/3 "2020-02-17T10:37:42Z")

</div>

@zivy  
great, that works! thank you!

---

<div class="post-metadata">

**Author:** ![fepegar](https://discourse.itk.org/user_avatar/discourse.itk.org/fepegar/32/1664_2.png) [@fepegar](https://discourse.itk.org/u/fepegar)\
**Post date:** [August 13, 2025, 3:17pm UTC](https://discourse.itk.org/t/limit-number-of-threads-in-python-simpleitk-programatically/2723/4 "2025-08-13T15:17:08Z")

</div>

Sorry for vamping this old thread, but it might be relevant.

@zivy, do you recommend setting this to 1 in a multiprocessing context? (e.g. PyTorch `DataLoader` with `num_workers` \> 1).

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [August 13, 2025, 4:08pm UTC](https://discourse.itk.org/t/limit-number-of-threads-in-python-simpleitk-programatically/2723/5 "2025-08-13T16:08:36Z")

</div>

Hello @fepegar,

No worries about reviving an old thread. I do recommend setting this to 1 when doing multiprocessing.

I’ve had bad experiences when multi-threading and multi-processing are done at the same time. It was painful enough that I [documented it here](https://github.com/InsightSoftwareConsortium/SimpleITK-Notebooks/blob/fd7fe1fd2ddbf299b31f90c0e424977b15ec16f1/Python/scripts/characterize_data.py#L1058-L1064). I now realize we should make this much more visible and will add it to the SimpleITK FAQ, so thanks for the question.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [August 13, 2025, 4:47pm UTC](https://discourse.itk.org/t/limit-number-of-threads-in-python-simpleitk-programatically/2723/6 "2025-08-13T16:47:11Z")

</div>

The other thing to remember with multi-processing in python is that is can be implemented in multiple ways. If the multi-processing is done with a “fork” from the main process the ITK number of threads will be set in the subprocess. However, with other schedulers like some available for Dask, a whole new process can be initialized with and consideration for how the new process is setup and important need to be made.

---

<div class="post-metadata">

**Author:** ![fepegar](https://discourse.itk.org/user_avatar/discourse.itk.org/fepegar/32/1664_2.png) [@fepegar](https://discourse.itk.org/u/fepegar)\
**Post date:** [August 13, 2025, 6:44pm UTC](https://discourse.itk.org/t/limit-number-of-threads-in-python-simpleitk-programatically/2723/7 "2025-08-13T18:44:23Z")

</div>

> [@zivy](#):
>
> Hello @fepegar,
> 
> No worries about reviving an old thread. I do recommend setting this to 1 when doing multiprocessing.
> 
> I’ve had bad experiences when multi-threading and multi-processing are done at the same time. It was painful enough that I [documented it here](https://github.com/InsightSoftwareConsortium/SimpleITK-Notebooks/blob/fd7fe1fd2ddbf299b31f90c0e424977b15ec16f1/Python/scripts/characterize_data.py#L1058-L1064). I now realize we should make this much more visible and will add it to the SimpleITK FAQ, so thanks for the question.

Thank you, @zivy. I’ve been suspecting for a while that this is the case, so I’m glad to hear this clear guidance.

> [@blowekamp](#):
>
> The other thing to remember with multi-processing in python is that is can be implemented in multiple ways. If the multi-processing is done with a “fork” from the main process the ITK number of threads will be set in the subprocess. However, with other schedulers like some available for Dask, a whole new process can be initialized with and consideration for how the new process is setup and important need to be made.

Thanks, @blowekamp. I’m not sure I follow 100%. For someone that doesn’t know much about concurrency, do you have suggestions on how/whether to set the number of threads for each scheduler/multiprocessing context?
