So in Slicer version 5.6.2, can i use the the native Python multi-processing methods? I test a sample code in the slicer python console ,and i find multi_thread method words very well, but multi_process method will crash the slicer.
import math
import time
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
# 测试的大素数列表
PRIMES = [112272535095293] * 10 # 重复多次以延长计算时间
# 判断一个数是否为素数
def is_prime(n):
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
sqrt_n = int(math.sqrt(n)) + 1
for i in range(3, sqrt_n, 2):
if n % i == 0:
return False
return True
# 单线程运行
def single_thread():
for number in PRIMES:
is_prime(number)
# 多线程运行
def multi_thread():
with ThreadPoolExecutor() as executor:
executor.map(is_prime, PRIMES)
# 多进程运行
def multi_process():
with ProcessPoolExecutor() as executor:
executor.map(is_prime, PRIMES)
# 测试三种方法的运行时间
if __name__ == "__main__":
for method, func in [("Single Thread", single_thread),
("Multi Thread", multi_thread),
("Multi Process", multi_process)]:
start_time = time.time()
func()
end_time = time.time()
print(f"{method} cost: {end_time - start_time:.2f} seconds")
In fact, I want to speed up the processing of extracting multimodal radiomics features. Currently, I am primarily using the following approach to call the CLI module:
self.cliNode = slicer.cli.run(slicer.modules.slicerradiomicscli, self.cliNode, cliParams,wait_for_completion=self.runSync)
As a result, I hope to call multiple CLI modules simultaneously. However, testing with Python’s multiprocessing seems not to work. Therefore, I would like to ask two questions:
- Can Python’s native
multiprocessingbe used in this scenario? - Is it possible to call CLI modules in parallel?"