我試圖從本地保存的musicbrainz數據庫中隨機獲得100K首歌曲。我是編程新手,想知道計算機放慢速度的原因是什麼(可能是內存填充)。我在虛擬機上運行ubuntu。請提出一些改變,讓我可以在運行後進入睡眠狀態。運行此.py文件後,我的電腦逐漸變慢
import psycopg2
import random
import MySQLdb
from contextlib import closing
conn = psycopg2.connect("dbname='musicbrainz' user='musicbrainz' host='localhost' password='musicbrainz'")
conn1 = MySQLdb.connect(host = "localhost", user = "root", passwd = "40OZlike", db = "plalyst")
print("connections and cursors made...")
cur= conn1.cursor()
conn1.set_character_set('utf8')
cur.execute('SET NAMES utf8;')
cur.execute('SET CHARACTER SET utf8;')
cur.execute('SET character_set_connection=utf8;')
cur.close()
def migrateSongDB():
try:
cur1 = conn1.cursor()
cur1.execute("select count(*) from Song")
numberOfSongs = cur1.fetchall()[0][0]
cur1.close()
print("number of songs in our database is ")
print(numberOfSongs)
rnumbers = random.sample(range(1, 22660511), 100000-numberOfSongs)
print("random numbers generated....")
for eachnum in rnumbers:
cur = conn.cursor()
cur1 = conn1.cursor()
print(eachnum)
songName=""
while(songName==""):
cur.execute("""select name from track where id = %s """, (eachnum,))
rows = cur.fetchall()
print(rows)
if not len(rows)==0:
songName = rows[0][0]
eachnum+=1
print("Got the track name:")
print(songName)
sql = 'INSERT into Song (name) values ("'+songName+'")'
print(sql)
cur1.execute(sql)
cur1.execute('commit')
print("inserted into the song table....")
cur.close()
cur1.close()
print("Songs Saved into new Data Base...")
conn.close()
conn1.close()
print("Connections Closed")
except:
with conn1 as cursor:
cursor.execute('select 1;')
result = cursor.fetchall()
for cur in result:
print(cur)
migrateSongDB()
def main():
migrateSongDB()
conn.close()
conn1.close()
if __name__ == "__main__": main()
感謝您抽出時間閱讀此代碼。 此外,如果你們有任何改善我的編碼風格的建議,我很樂意學習。 再次感謝您。
使用常用系統工具(如top)識別放緩的原因。 –
@KlausD:'top'在這裏會變得毫無價值。 – Makoto
@Makoto ...你知道嗎? –