我在Windows 10上使用python 3.7(64位)。我的脚本运行良好,有时在写入数据库时会冻结。有时它会写超过10万行,有时会冻结在5000左右。这只是随机的,我不知道为什么。
这是我的代码:
import mysql.connector
import datetime
from math import log2
import time
corporaDb = mysql.connector.connect(
host="localhost",
user="root",
passwd="root"
)
dictionaryDb = mysql.connector.connect(
host="localhost",
user="root",
passwd="root",
database="dictionaries"
)
dictionaryDatabaseName = "dictionaries"
corpus = "deu_news_2015"
corporaCursor = corporaDb.cursor()
dictionaryCursor = dictionaryDb.cursor()
lang = corpus[:3]
print(lang)
print(datetime.datetime.now().time())
query = "SELECT freq FROM "+corpus+".words order by freq desc LIMIT 1"
corporaCursor.execute(query)
freqMax= corporaCursor.fetchone()
freqMax = freqMax[0]
query = "SELECT w.freq, t.translation_id FROM "+dictionaryDatabaseName+".word_translations t INNER JOIN "+corpus+".words w ON t.translation = w.word where target_language = '"+lang+"' LIMIT 2700000, 2000000"
corporaCursor.execute(query)
result = corporaCursor.fetchall();
i= 0
for row in result:
if(i % 1000 == 0):
print(i)
if(row[0] != "None"):
try:
frequencyClass = round(log2(freqMax/row[0]))
query = "UPDATE word_translations SET freq = "+str(row[0])+", available = 1, freq_class = "+str(frequencyClass)+" WHERE translation_id = '"+str(row[1])+"'"
dictionaryCursor.execute(query)
if(i % 100 == 0):
dictionaryDb.commit()
except:
dictionaryCursor.close()
print ("fehler")
time.sleep(30)
dictionaryCursor = dictionaryDb.cursor()
pass
i+=1
corporaDb.close()