0

nice to meet you, let me show you a simplified code first

from multiprocessing import Pool

AAA = []

def input_list():
    with open('./AAA.tsv') as f:
        for line in f:
            AAA.append(line[:9])
        return AAA


def printf(number):
   print(AAA)



if __name__ == '__main__':
    input_list()
    pool = Pool(processes=1)
    pool.map(printf, range(0, AAA.__len__()))

this is done, the AAA executed due to input_list will be It is not recognized in printf method.

However, if you declare input_list() outside,

from multiprocessing import Pool

AAA = []

def input_list():
    with open('./AAA.tsv') as f:
        for line in f:
            AAA.append(line[:9])
        return AAA


def printf(number):
    print(AAA)


input_list()

if __name__ == '__main__':
    pool = Pool(processes=1)
    pool.map(printf, range(0, AAA.__len__()))

It works properly. Why does this happen?

1 Answers1

1

As the link posted by @jonrsharpe mentions, if __name__ == '__main__': in python is meant to make the content of that if-block only run if the module is being executed directly, and not read as part of an import statement (ie. the module was written to be used as a library).

If you are used to C/C++/Java, you are probably used to main() being the absolute entry point to your program. That's not how it works in Python. Python behaves a lot more like Bash or other so-called "scripting" languages in this regard: when you run a program from the command line (by typing ./my_prog.py for example), the Python interpreter reads the entire file, from top to bottom, and executes each statement sequentially.

So, in the case where you run the program directly, it doesn't really matter what you put in that if block, versus what is outside. As long as the statements are in the same order, and the logic of the program is otherwise the same, they will run in the same sequence and produce the same behavior.

Also note that, when we say that the interpreter "runs each statement sequentially", this doesn't necessarily mean that every statement is immediately executed. For example, anything that falls within the scope of a def my_func(): block will be treated as part of that function definition and will only run if you later call that function.

Also note that this can have consequences with respect to the order in which you define and use variables/function, or any object. Things must be defined before they can be used, so this will not work:

x()
def x():
  print('hello, world!')
Z4-tier
  • 7,287
  • 3
  • 26
  • 42