D
D
Dima_E2019-10-10 16:27:19
Python
Dima_E, 2019-10-10 16:27:19

Multiprocessing why executes the main code?

print("начало")

def aa():
    time.sleep(1)
    print("Конец кода")


if __name__ == '__main__':
    p = Process(target=aa)
    p2 = Process(target=aa)
    p3 = Process(target=aa)
    p.start()
    p2.start()
    p3.start()

In this example, when starting multiprocessing, in addition to the function that is taken in the target, it executes the main code, why ?
The output of this code will be:
начало
начало
начало
начало
Конец кода
Конец кода
Конец кода

this happens to everything, if there are mouse clicks it clicks every time you start multiprocessing , how to fix it ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-10-10
@Dima_E

Because each new process is a separate Python interpreter that re-imports the script, and when importing, all top-level instructions are executed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question