I created a simple Python script to log track listens in iTunes and it seems that it's pretty inefficient. I'm primarily a front-end developer, so this is clearly not my area of expertise. I know loops can cause problems in any language, so I assume that's the issue here.
If I run this script for a while (say, a couple of hours or more) the fan on my computer starts working overtime and won't stop until I kill the script.
Any idea what might be making this happen? Suggestions on how to optimize? Appreciate any help I can get.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx, sqlite3, datetime, threading
from appscript import *
it = app('iTunes')
from AppKit import *
# Create the DB
Tapedeck = sqlite3.connect('Tapedeck.db', check_same_thread = False)
TapedeckQuery = Tapedeck.cursor()
Tapedeck.execute('''CREATE TABLE IF NOT EXISTS plays (id INTEGER PRIMARY KEY, track_id TEXT, track TEXT, artist TEXT, album TEXT, genres TEXT, play_count INTEGER, rating INTEGER, skip_count INTEGER, year_released INTEGER, date_played DATE)''')
Tapedeck.commit()
def getSec(s):
l = s.split(':')
return int(l[0]) * 60 + int(l[1])
def addTrack(track_id, track, artist, album, genres, play_count, skip_count, year_released, date_played, rating):
#print ' --\n Position: {}\n Length: {}'.format(it.player_position(),getSec(it.current_track.time()))
TapedeckQuery.execute('''INSERT INTO plays (track_id, track, artist, album, genres, play_count, skip_count, year_released, date_played, rating) VALUES (?,?,?,?,?,?,?,?,?,?)''',
(track_id, track, artist, album, genres, play_count, skip_count, year_released, date_played, rating))
Tapedeck.commit()
loop_int = 1.0
last_track = '0'
def listen():
global loop_int, last_track
threading.Timer(loop_int, listen).start()
if it.player_state() == k.playing:
# check to see if track was restarted
if it.player_position() < getSec(it.current_track.time())/2 and last_track == it.current_track.persistent_ID():
self.last_track = '0'
# has the track played beyond the halfway mark?
if it.player_position() >= getSec(it.current_track.time())/2 and last_track != it.current_track.persistent_ID():
today = datetime.datetime.today()
now = '{}-{}-{} {}:{}:{}'.format(today.year, '%02d' % today.month, '%02d' % today.day, '%02d' % today.hour, '%02d' % today.minute, '%02d' % today.second)
print '\nArtist: {}\nTrack: {}\nAlbum: {}\nGenres: {}\nDatetime: {}'.format(it.current_track.artist().encode('ascii','ignore'), it.current_track.name().encode('ascii','ignore'), it.current_track.album().encode('ascii','ignore'), it.current_track.genre().encode('ascii','ignore'), now)
last_track = it.current_track.persistent_ID()
addTrack(it.current_track.persistent_ID(), it.current_track.name(), it.current_track.artist(), it.current_track.album(), it.current_track.genre(), it.current_track.played_count(), it.current_track.skipped_count(), it.current_track.year(), now, it.current_track.rating())
listen()
python -m cProfile -o prof.dat <prog> <args>
for a while, and you'll get file that gives you calls and how long each call took in CPU seconds. To view it interactively, you would wantpython -m pstats prof.dat
. Take a look at that, then see if you can isolate the parts in which performance is acting up. A sorted (by cumulative time) .dat file would be helpful to isolate any issues as well. \$\endgroup\$