This was already answered in #mopidy on freenode, but since others might find the question here is the answer and some explanation.
libspotify and thus pyspotify is not thread safe. There is global state for the library so the setup of the global session need to be moved so that it is only done once a startup. A typical way to do this in django is to move it to the __init__
method for a middleware, and then attach the global state to each request.
class SpotifyMiddlware(object):
def __init__(self):
config = spotify.SessionConfig()
config.application_key_filename = os.path.join(
os.path.dirname(os.path.realpath(__file__)), 'spotify_appkey.key')
self.session = spotify.Session(config=config)
self.session.login('TehNanor', '*******')
while not self.session.user:
self.session.process_events()
def process_request(self, request):
request.spotify = self.session
Waiting for the login to complete could also be done doing a callback + a threading event. Our you could simply check the login state in your views, or even in the middleware's process_request