简体   繁体   中英

Python multiprocessing take long time to finish

Hi, I start with the multiprocessing of python

I have a simple code like this:

import multiprocessing as mp
import time

def print_screen():
    print "Hello world"

def main():
    p1 = mp.Process(target=print_screen())
    p2 = mp.Process(target=print_screen())

    p1.start()
    p2.start()

    p1.join()
    p2.join()


if __name__ == '__main__':
    start_time = time.time()
    main()
    print "run time " + str(time.time() - start_time)

And the result is:

Hello world
Hello world
run time 2.06299996376

Can you explain to me why these codes take a long time to complete, even though the two "Hello world" rows are printed right after running the program. The run time will increase as I increase the number of processes. Thanks very much.

Your runtime will vary greatly, depending on what machine you run this code. What is taking so long is the time it actually takes the system you're running on to start two entirely separate processes, execute them and then stop them and clean them up again.

Using processes has the advantage of allowing you to use multiple cores, running the task in an entirely separate process, but it has the disadvantage of this overhead of creating, starting, stopping and cleaning up a process. For tasks that don't need to truly run in parallel, but might be using various resources that they need to wait on (like file or network I/O), threads can be a better and much faster solution than processes, if that's a consideration.

Also, as a general remark: if you're writing new code, probably do it in Python 3, not 2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM