TGStat
TGStat
Type to search
Advanced channel search
  • flag English
    Site language
    flag Russian flag English flag Uzbek
  • Sign In
  • Catalog
    Channels and groups catalog Regional compilations Thematic compilations Платные каналы Search for channels
    Add a channel/group
  • Ratings
    Rating of channels Rating of groups Posts rating
    Ratings of brands and people
  • Analytics
  • Search by posts
  • Telegram monitoring
  • Promotion
    Advertising through Yandex Business Advertising in channels through TGStat Agency Advertising on TGStat.ru website
Библиотека Python разработчика | Книги по питону

16 Sep, 22:46

Open in Telegram Share Report

Если у вас есть ресурсоемкая задача для процессора и вы хотите использовать все доступные ядра, то multiprocessing.Pool - это то, что вам нужно. Он создает несколько процессов и автоматически распределяет между ними задачи. Просто создайте пул с Pool(number_of_processes) и выполните p.map с списком входных данных.


import math
from multiprocessing import Pool

inputs = [i ** 2 for i in range(100, 130)]

def f(x):
return len(str(math.factorial(x)))

# Однопоточное выполнение
%timeit [f(x) for x in inputs]
# 1.44 s ± 19.2 ms per loop (...)

# Параллельное выполнение с 4 процессами
p = Pool(4)
%timeit p.map(f, inputs)
# 451 ms ± 34 ms per loop (...)


Также можно не указывать параметр number_of_processes, по умолчанию он равен количеству ядер CPU в системе.

📲 Мы в MAX

👉@BookPython

1.2k 0 5 2
Catalog
Channels and groups catalog Channels compilations Search for channels Add a channel/group
Ratings
Rating of Telegram channels Rating of Telegram groups Posts rating Ratings of brands and people
API
API statistics Search API of posts API Callback
Our channels
@TGStat @TGStat_Chat @telepulse @TGStatAPI
Read
Академия TGStat Telegram Research 2019 Telegram Research 2021 Telegram Research 2023
Contacts
Справочный центр Support Email Jobs
Miscellaneous
Terms and conditions Privacy policy Public offer
Our bots
@TGStat_Bot @SearcheeBot @TGAlertsBot @tg_analytics_bot @TGStatChatBot