我有以下腳本執行了三次,但我不知道為什么#!C:/Python38-32/python.exe#script.pyfrom py_get_data_from_game import mainfrom multiprocessing import Poolimport connect_to_dbimport mysql.connectorfrom mysql.connector import Errorconnection = 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.pySTARTMySQL connection is closedSTARTMySQL connection is closedSTARTMySQL connection is closed
1 回答

蠱毒傳說
TA貢獻1895條經驗 獲得超3個贊
當您使用時multiprocessing
,python 需要創建與您的程序碰巧指定的進程一樣多的進程,并具有相同的環境(我的意思是imports
)。它通過再次運行您的腳本來完成此操作。
為避免同時執行虛假代碼,您應該將其放在:
if __name__ == '__main__':
添加回答
舉報
0/150
提交
取消