Python processes

Asked

Viewed 239 times

2

I’m studying the module multiprocessing and in all the examples in python documentation there is always a if verification in the examples, I know this checks whether the file is running directly or being imported but has some relation to the initialization of a processo?

The multiprocessing module also introduces Apis which do not have Analogs in the threading module. A prime example of this is the Pool Object which offers a convenient Means of parallelizing the Execution of a Function Across Multiple input values, Distributing the input data Across processes (data parallelism). The following example demonstrates the common Practice of Defining such functions in a module so that Child processes can successfully import that module. This basic example of data parallelism using Pool,

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    with Pool(5) as p:
        print(p.map(f, [1, 2, 3]))
  • "whether the module has been imported or not", where this?

  • The issues helped a lot to understand your question. I create that 'if' usage name == 'main' ' Be just to leave more didactic the example given in the documentation. I may be wrong. I will follow the post

  • "whether the module was imported or not", - yes, that’s exactly it. - the condition __name__== '__main__' is true when the module is the main file running in Python’s Runtime - there the variable __name__ takes on the value __main__ - if the current module is imported from another Python module this variable will have the name of the module.

1 answer

1

This condition causes the Pool is started only if the program is called directly, as the module multiprocessing will also call this program to run the function f() for each instance of pool this prevents the boot chunk from running a second time and producing a loop infinite.

  • 1

    In fact, multiprocessing uses fork (in operating systems, in DOS do not know how to say) - then the mechanism is different - it may even be that if you test the variable __name__ within a multiprocessing worker it is __main__ also. Anyway, that block runs only in the main process. But nothing prevents you from using this block just to call a "main" function that has all the logic - there doesn’t need to be more code inside it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.