我有以下脚本执行了三次,但我不知道为什么#!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
提交
取消