我有以下腳本執(zhí)行了三次,但我不知道為什么#!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貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊
當(dāng)您使用時(shí)multiprocessing
,python 需要?jiǎng)?chuàng)建與您的程序碰巧指定的進(jìn)程一樣多的進(jìn)程,并具有相同的環(huán)境(我的意思是imports
)。它通過再次運(yùn)行您的腳本來完成此操作。
為避免同時(shí)執(zhí)行虛假代碼,您應(yīng)該將其放在:
if __name__ == '__main__':
添加回答
舉報(bào)
0/150
提交
取消