Обложка канала

Библиотека Python разработчика

20835 @BookPython

Библиотека Python разработчика. Книги по программированию на Python.

Библиотека Python разработчика

4 года назад
Открыть в
If you have a CPU-heavy task and want to utilize all the cores you have, then multiprocessing.Pool is for you. It spawns multiple processes and delegates tasks to them automatically. Simply create a pool with Pool(number_of_processes) and run p.map with the list of inputs. In : import math In : from multiprocessing import Pool In : inputs = [i ** 2 for i in range(100, 130)] In : def f(x): ...: return len(str(math.factorial(x))) ...: In : %timeit [f(x) for x in inputs] 1.44 s ± 19.2 ms per loop (...) In : p = Pool(4) In : %timeit p.map(f, inputs) 451 ms ± 34 ms per loop (...) You can also omit the number_of_processes parameter, the default value for it is the number of CPU cores on the current system.