Skip to content

Commit bc162de

Browse files
author
Zhiyuan Liu
committed
ENH: Add a convenient function to get/set the number of threads.
1 parent aedde4a commit bc162de

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

Wrapping/Generators/Python/Tests/extras.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ def custom_callback(name, progress):
3030
import sys
3131
import os
3232

33+
# test setting the number of threads
34+
itk.set_nthreads(4)
35+
assert itk.get_nthreads() == 4
36+
3337
# test the force load function
3438
itk.force_load()
3539

Wrapping/Generators/Python/itkExtras.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@
2727
# [K erases the end of the line
2828
clrLine = "\033[2000D\033[K"
2929

30+
def set_nthreads(number_of_threads):
31+
"""
32+
Support convenient set of the number of threads.
33+
Use example (in python):
34+
import itk
35+
itk.set_nthreads(4) ## use 4 threads
36+
"""
37+
assert number_of_threads > 0, "Please set a possitive number of threads instead of %d" % number_of_threads
38+
39+
import itk
40+
threader = itk.MultiThreaderBase.New()
41+
threader.SetGlobalDefaultNumberOfThreads(number_of_threads)
42+
43+
def get_nthreads():
44+
"""
45+
Get the number of threads
46+
"""
47+
import itk
48+
threader = itk.MultiThreaderBase.New()
49+
return threader.GetGlobalDefaultNumberOfThreads()
3050

3151
def auto_not_in_place(v=True):
3252
"""Force it to not run in place

0 commit comments

Comments
 (0)