Multiprocessing in Python

1

I'm creating processes using multiprocessing in Python, but I need to create processes inside other processes, that is, child processes. Is it possible to do this using multiprocessing ?

Code:

import time
import multiprocessing

def calc_square():
    while 1:
        time.sleep(1)
        print("square: %i" %(3*3*3))

def calc_cube():
    while 1:
        time.sleep(1)
        print("cube: %i" %(2*2*2))
        p2 = multiprocessing.Process(target=calc_square)
        p2.start()
        p2.join()

if __name__ == '__main__':
    p1 = multiprocessing.Process(target=calc_cube)
    p1.start()
    p1.join()

When creating the calc_cube process, it gives print to cube once and creates the calc_square process, and only shows the print of calc_square , ie the two processes do not run in parallel.

Is it possible to run in parallel?

    
asked by anonymous 05.10.2018 / 03:41

1 answer

4

There is no restriction on the use of multiprocessing from within a process created by multiprocessing itself. But your system is doing exactly what you tell it to do:

From within calc_cube , it sleeps a second, prints the cube once, starts the process calc_square and waits for calc_square to finish . Since this other is in an infinite loop and never ends, only it will print answers. What is the surprise?

If you reorder the instructions inside calc_cube in order to make sense: create a single calc_square process (and do not try to create a new process with each loop execution), and print your results without worrying about waiting for the other process, you will see the two impressions happening simultaneously:

def calc_cube():
    p2 = multiprocessing.Process(target=calc_square)
    p2.start()  # inicia uma única instância do outro processo
    while True:
        time.sleep(1)
        print("cube: %i" %(2*2*2))

    p2.join()  # essa linha nunca será atingida - pois está fora do while. Se o código mudar para o calc_cube ter um fim, aí sim ela faz sentido.
Ready - this will work and you can test it on your PC - you do not have to bother putting it in the rasp. Now - you have to bear in mind that you will not have many advantages of using such strategies - multiprocessing is cool if you have multi-core CPUs, and even so, up to the limit of 1 process per core, for heavily calculated algorithms , or 2 processes per core, "bursting". The operating system features that processes use are very large and you will have advantages, especially on a single-core machine, if you use asyncio - a single thread quickly switching between code that always has something to do, while other parts of the code expect answers of I / O.

Here I wrote an answer where I extensively treated the topic of code parallelism in Python: #

    
05.10.2018 / 04:24