惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
L
LangChain Blog
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tailwind CSS Blog
N
Netflix TechBlog - Medium
Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
博客园 - 【当耐特】
量子位
有赞技术团队
有赞技术团队
Jina AI
Jina AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
C
Check Point Blog
B
Blog RSS Feed
M
MIT News - Artificial intelligence
H
Help Net Security
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 聂微东
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
A
About on SuperTechFans
腾讯CDC

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Using Multiprocessing in Python
2020-07-27 · via jdhao's digital space

One way to achieve parallelism is to use multi-processing, where we can execute tasks in different cores of the CPU to reduce the total processing time. Python provides the multiprocessing package to facilitate this.

This post summarizes some of the questions I have when I learn to use multiprocessing in Python.

How many processes should we use?#

For CPU-bound tasks, set the process number to the number of CPU cores is perhaps appropriate. To get the number of CPUs in your system:

import multiprocessing as mp

print(mp.cpu_count())

However, the time to complete our task will stop decreasing when the number of processes reach a certain number due to other factors. So it is a good idea to do some benchmark to find the optimal number of processes to use.

Ref:

Do I need to use pool.close() and pool.join() after finishing my tasks?#

pool.close() makes sure that process pool does not accept new processes, and pool.join() waits for the processes to properly finish their work and return. So it is a good idea to use pool.close() and pool.join() explicitly.

Otherwise, the processes may not be released properly. In our application, we have seen error related to memory usage:

OSError: [Errno 12] Cannot allocate memory

Ref:

Difference between pool.map() and pool.map_aysnc()?#

pool.map is blocking until it gets the actual results, that is, it will block the execution of code until it gets all the result from the processes. pool.map_async, on the other hand, is non-blocking and will return immediately and all we get is an AsyncResult object. To get the actual result, we use result.get() to retrieve them. To illustrate their differences:

import multiprocessing as mp

def toy(x):
    print("function gets executed.")
    return x*x

def main():
    p = mp.Pool()

    result = p.map(toy, [1])
    print("after p.map")
    print(result)

    res = p.map_async(toy, [1])
    print("after p.map_async()")
    print(res.get())


if __name__ == "__main__":
    main()

If you use p.map(), you will see the following result:

function gets executed.
after p.map()
[1]

This is because p.map() is blocking. So the statement following it won’t be executed if p.map() hasn’t got the results back.

Unlike p.map, p.map_async is non-blocking. The output for second part of the code is:

after p.map_async()
function gets executed.
[1]

That is because p.map_async will not wait for the function to be executed and returned. So you see the output after p.map_async() first. Then you see function gets executed..

pool.map() without argument?#

The function pool.map() is used to feed the element of an iterable to a function one by one. We can not use it to run functions without argument. However, we may change the function to accept an argument and ignore that argument. Or we can write a wrap function to accept argument and invoke the original function in the wrap function.

import multiprocessing as mp

def f():
    print("function without argument")

def wrap_func(x):
    f()

p = mp.Pool()
p.map(wrap_func, range(10))

Ref:

functions with multiple arguments?#

pool.map() can only execute functions that accept one argument, to run a function that accepts multiple arguments, we can use pool.starmap():

import multiprocessing as mp


def func(x, y):
    return x * y

p = mp.Pool()

l1 = range(1, 10)
l2 = range(10, 19)

res = p.starmap(func, zip(l1, l2))
print(res)

If the other parameters of the function are constants, it may be convenient to use partial functions instead:

from functools import partial

l1 = range(1, 10)
# If one of the parameters is constant.
partial_func = partial(func, y=2)
res = p.map(partial_func, l1)
print(res)