Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added threading to search methods #194

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

allerter
Copy link
Collaborator

@allerter allerter commented Mar 7, 2021

The search_album and search_artist methods could definitely benefit from threading. I added the num_workers parameter for the user to define the number of threads used. If the user just used the default num_workers=1, no threading is used and the songs are downloaded like they were before.
As for thread safety:

  • Python lists are inherently thread-safe and we just add the created songs to the list. So I don't see any issues with artist.songs.
  • The artist.num_songs attribute isn't thread-safe and we'd need to set setter/getters and a threading.lock if we wanted to keep that attribute. Instead, I removed it and substituted it with len(artist.songs).
  • The requests.Session object is not thread-safe and it's recommended to use one Session per thread. But considering that we don't modify the Session at all in this solution, I don't think that'll be a problem. Although it needs a little more investigation.

Checklist

  • Handle exceptions in threads
  • Add unittests
  • Update/Add docs

When using threads, songs are not guaranteed to be inserted in order, so they need to be sorted
sublass therading.Thread, run the thread and if it resulted in an exception, put that error in an errors_queue. Then check the queue after a page is done and raise if there are any errors
- cleared thread_pool list for next pages
- moved print statement in search_artist to after songs have been fetched. When threads are used, this statement would get printed before songs are done being fetched
Set thread.daemon to True so that they're killed when an error is raised
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant