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?
– Woss
Related or duplicate: Use of if __name__=='__main__'. Why class is not instantiated?
– Woss
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
– Otávio Reis Perkles
"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.– jsbueno