TGViewer
Добро пожаловать в мир Python Добро пожаловать в мир Python @welcome_python · 2.76K subscribers
Post #599 1.23K
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 (...)
  • 👍 2
More from @welcome_python
  1. Apr 12, 2026СКОРО: D-строки в Python PEP 822 предлагает новый синтаксис «d-строки» (многострочной стро…
  2. Aug 12, 2025KittenTTS — это сверхлёгкая модель преобразования текста в речь с открытым исходным кодом,…
  3. Aug 1, 2025ЕДИНСТВЕННАЯ дорожная карта по науке о данных Простая и эффективная дорожная карта для изу…
  4. Aug 1, 2025Учебное пособие по голосовому агенту Python AI — полное руководство разработчика (Deepgram…
  5. Aug 1, 2025Полное руководство по созданию и развертыванию ИИ-агента с использованием контейнеров Dock…
  6. Aug 1, 2025Разработка панели мониторинга в реальном времени с использованием FastAPI, Postgres и WebS…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →