ThreadPool and Pool for parallel processing

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


ThreadPool and Pool for parallel processing



Is there a way to use both ThreadPool and Pool in python to parallelise a loop by specifying the number of CPUs and cores you wish to use?



For example I would have a loop execute as:


from multiprocessing.dummy import Pool as ThreadPool
from tqdm import tqdm
import numpy as np

def my_function(x):
return x + 1

pool = ThreadPool(4)
my_array = np.arange(0,1e6,1)


results = list(tqdm(pool.imap(my_function, my_array),total=len(my_array)))



For 4 cores (threads) but it I wanted to spread these out on multiple CPUs as well, is there a simple way to adapt the code?




1 Answer
1



You can just use it with no parameter and let the library decide:


from multiprocessing.pool import ThreadPool
...
pool = ThreadPool()





But that will not share the process across multiple CPUs no? Only cores
– user8188120
11 mins ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

xPJb5XMTFL,lAmoEX4SZxsTn2rP 5ctkMQw z3wmCJ,Tk5YNC7p3ByYUpJDOYjtNgXwogrRlgSHOBch,yikaFXPEXf0sy
fT,ZaM9b

Popular posts from this blog

Makefile test if variable is not empty

Visual Studio Code: How to configure includePath for better IntelliSense results

Will Oldham