Telegram Group & Telegram Channel
Если у вас есть ресурсоемкая задача для процессора и вы хотите использовать все доступные ядра, то 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 в системе.

👉@BookPython



tg-me.com/BookPython/3557
Create:
Last Update:

Если у вас есть ресурсоемкая задача для процессора и вы хотите использовать все доступные ядра, то 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 в системе.

👉@BookPython

BY Библиотека Python разработчика | Книги по питону


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/BookPython/3557

View MORE
Open in Telegram


Библиотека Python разработчика Telegram | DID YOU KNOW?

Date: |

The SSE was the first modern stock exchange to open in China, with trading commencing in 1990. It has now grown to become the largest stock exchange in Asia and the third-largest in the world by market capitalization, which stood at RMB 50.6 trillion (US$7.8 trillion) as of September 2021. Stocks (both A-shares and B-shares), bonds, funds, and derivatives are traded on the exchange. The SEE has two trading boards, the Main Board and the Science and Technology Innovation Board, the latter more commonly known as the STAR Market. The Main Board mainly hosts large, well-established Chinese companies and lists both A-shares and B-shares.

NEWS: Telegram supports Facetime video calls NOW!

Secure video calling is in high demand. As an alternative to Zoom, many people are using end-to-end encrypted apps such as WhatsApp, FaceTime or Signal to speak to friends and family face-to-face since coronavirus lockdowns started to take place across the world. There’s another option—secure communications app Telegram just added video calling to its feature set, available on both iOS and Android. The new feature is also super secure—like Signal and WhatsApp and unlike Zoom (yet), video calls will be end-to-end encrypted.

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


Telegram Библиотека Python разработчика | Книги по питону
FROM USA