为什么我的python脚本执行了三次?

为什么我的python脚本执行了三次?,python,Python,下面的脚本执行了三次,但我不知道为什么 #!C:/Python38-32/python.exe #script.py from py_get_data_from_game import main from multiprocessing import Pool import connect_to_db import mysql.connector from mysql.connector import Error connection = connect_to_db.connect() u

下面的脚本执行了三次,但我不知道为什么

#!C:/Python38-32/python.exe
#script.py
from py_get_data_from_game import main
from multiprocessing import Pool

import connect_to_db
import mysql.connector
from mysql.connector import Error

connection = connect_to_db.connect()

user_names = []
passwords = []
    
print('START')  
try:
    connection = connect_to_db.connect()
    if connection.is_connected():
        sql_select_Query = "select * from players"
        cursor = connection.cursor(dictionary=True)
        cursor.execute(sql_select_Query)
        records = cursor.fetchall()

        for row in records:
            user_names.append(row['user_name'])
            passwords.append(row['password'])

except Error as e:
    print("Error while connecting to MySQL", e)
finally:
    if (connection.is_connected()):
        cursor.close()
        connection.close()
        print("MySQL connection is closed")
        
if __name__ == '__main__':
    pool = Pool(2) # two parallel jobs
    results = pool.map(main, zip(user_names, passwords))
输出:

C:\scripts>script.py
START
MySQL connection is closed
START
MySQL connection is closed
START
MySQL connection is closed

当您使用
多处理
时,python需要在相同的环境(我指的是
导入
)中创建与您的程序恰好指定的数量相同的进程。它通过再次运行脚本来实现这一点

为避免同时执行伪代码,应将其放在以下位置之后:

if __name__ == '__main__':

当您使用
多处理时,您需要将从
打印('START')
的所有内容都放在
的内部,如果uuuuuuu name\uuuuuuuu=='\uuuuuuu main\uuuuuuu':
。这很有效!你能解释一下为什么需要这个吗?(在我可以接受的答案中)这回答了你的问题吗?