Python multiprocessing speed

2024/5/20 12:58:35

I wrote this bit of code to test out Python's multiprocessing on my computer:

from multiprocessing import Poolvar = range(5000000)
def test_func(i):return i+1if __name__ == '__main__':p = Pool()var = p.map(test_func, var)

I timed this using Unix's time command and the results were:

real 0m2.914s
user 0m4.705s
sys  0m1.406s

Then, using the same var and test_func() I timed:

var = map(test_func, var)

and the results were

real 0m1.785s
user 0m1.548s
sys  0m0.214s

Shouldn't the multiprocessing code be much faster than plain old map?

Answer

Why it should.

In map function, you are just calling the function sequentially.

Multiprocessing pool creates a set of workers to which your task will be mapped. It is coordinating multiple worker processes to run these functions.

Try doing some significant work inside your function and then time them and see if multiprocessing helps you to compute faster.

You have to understand that there will be overheads in using multiprocessing. Only when the computing effort is significantly greater than these overheads that you will see it's benefits.

See the last example in excellent introduction by Hellmann: https://doughellmann.com/posts/pymotw-3-multiprocessing-manage-processes-like-threads/

pool_size = multiprocessing.cpu_count() * 2
pool = multiprocessing.Pool(processes=pool_size,initializer=start_process,maxtasksperchild=2,)
pool_outputs = pool.map(do_calculation, inputs)

You create pools depending on cores that you have.

https://en.xdnf.cn/q/73138.html

Related Q&A

Using os.forkpty() to create a pseudo-terminal to ssh to a remote server and communicate with it

Im trying to write a python script that can ssh into remote server and can execute simple commands like ls,cd from the python client. However, Im not able to read the output from the pseudo-terminal af…

Calculating the sum of a series?

This is my assignment and for the life of me i cant seem to think of a way to do it. This is the code I have so far:sum = 0 k = 1 while k <= 0.0001:if k % 2 == 1:sum = sum + 1.0/kelse:sum = sum - 1.…

get all the partitions of the set python with itertools

How to get all partitions of a set? For example, I have array [1, 2, 3]. I need to get [[1], [2], [3]], [[1], [2, 3]], [[2], [1,3]], [[3], [1, 2]], [[1, 2, 3]]. Now, I wrote this code:def neclusters(S…

Installing Python binary modules to a custom location in Windows

Suppose that I want to install a binary module for Python on Windows. Suppose that the module is distributed as a pre-built installer xxx-n.n.n.win32-py2.7.exe, prepared using distutils.My problem is t…

tkinter tkMessageBox html link

I got a tkMEssagebox.showerror showing up in a python tkinter application, when somebody failed to login with the application. Is it possible to have a url link in the tkMessageBox.showerror?ie.tkMess…

How to build a MultiIndex Pandas DataFrame from a nested dictionary with lists

I have the following dictionary.d= {key1: {sub-key1: [a,b,c,d,e]},key2: {sub-key2: [1,2,3,5,8,9,10]}}With the help of this post, I managed to successfully convert this dictionary to a DataFrame.df = pd…

Functions and if - else in python. Mutliple conditions. Codeacademy

Write a function, shut_down, that takes one parameter (you can use anything you like; in this case, wed use s for string).The shut_down function should return "Shutting down..." when it gets …

ipython up and down arrow strange behaviour

In my installation of ipython I have this strange problem where I cannot reliably move through command history with up and down arrows... a lot of the time it just doesnt work (nothing happens on the k…

Python for loop slows and evenutally hangs

Im totally new to Python (as of half an hour ago) and trying to write a simple script to enumerate users on an SMTP server.The users file is a simple list (one per line) of usernames.The script runs fi…

Jupyter Notebook: Change Data Rate Limit Inside Active Notebook

I have a jupyter notebook where an executed cell gives the following error:IOPub data rate exceeded...I understand this is an option:jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10However, …