Switch from time.sleep to threading.Event for making search non-async

This commit is contained in:
Stein Magnus Jodal 2010-02-15 22:59:29 +01:00
parent 0ab6a311e5
commit a2ec5ff5b4

View File

@ -42,21 +42,22 @@ class LibspotifyCurrentPlaylistController(BaseCurrentPlaylistController):
class LibspotifyLibraryController(BaseLibraryController): class LibspotifyLibraryController(BaseLibraryController):
search_results = False _search_results = None
_search_results_received = threading.Event()
def search(self, type, what): def search(self, type, what):
# XXX This is slow # FIXME This is slow, like 12-14s between querying and getting results
self.search_results = None self._search_results_received.clear()
query = u'%s:%s' % (type, what)
def callback(results, userdata): def callback(results, userdata):
logger.debug(u'Search results received') logger.debug(u'Search results received')
self.search_results = results self._search_results = results
query = u'%s:%s' % (type, what) self._search_results_received.set()
self.backend.spotify.search(query.encode(ENCODING), callback) self.backend.spotify.search(query.encode(ENCODING), callback)
while self.search_results is None: self._search_results_received.wait()
time.sleep(0.01)
result = Playlist(tracks=[self.backend.translate.to_mopidy_track(t) result = Playlist(tracks=[self.backend.translate.to_mopidy_track(t)
for t in self.search_results.tracks()]) for t in self._search_results.tracks()])
self.search_results = False self._search_results = None
return result return result