-
Notifications
You must be signed in to change notification settings - Fork 0
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
chore: Fix background tasks blocking UI #797
chore: Fix background tasks blocking UI #797
Conversation
9b9fd35
to
4e5526a
Compare
Codecov Report
@@ Coverage Diff @@
## main #797 +/- ##
==========================================
+ Coverage 81.94% 82.41% +0.46%
==========================================
Files 64 65 +1
Lines 5743 5754 +11
==========================================
+ Hits 4706 4742 +36
+ Misses 1037 1012 -25
📣 Codecov offers a browser extension for seamless coverage viewing on GitHub. Try it in Chrome or Firefox today! |
uses_collection=False
for chosen background tasks6711911
to
4e5526a
Compare
e9fa39a
to
f98bf7a
Compare
f98bf7a
to
c32218c
Compare
already running in parallel, so there is no need to do anything. | ||
This way we can use the same code for all Anki versions. | ||
""" | ||
if ANKI_MINOR < 231000: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🌱 Since this is a value used in multiple places, maybe is a good idea to store this in a constant that can be imported by different modules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't talking about the ANKI_MINOR
, I'm talking about the value 231000. This value is kind of a magic number, without context is almost impossible to know why this value is important. So I'm suggesting changing this value to a constant that adds some context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anki 23.10 serializes background tasks by default and
uses_collection=False
needs to be specified in order to allow the task to be run in parallel with other tasks (by Anki's task manager which is defined inaqt.mw.taskman
).The add-on is using
aqt.taskman.TaskManager.run_in_background
in multiple places to start background tasks. Due to the changes introduced in Anki 23.10 some background tasks can now block the UI.For example, when the media sync is in progress and then the user selects a deck on the Anki main window, a progress dialog opens and blocks the UI like this until the media sync is done:
This is because Anki calls
set_current_deck()
as aCollectionOp
in the background andset_current_deck
can’t be run until the media sync is done.To fix this, we can specify
uses_collection=False
for these background tasks so that the task manager (aqt.mw.taskman
) runs the task in parallel with other tasks. We can do this for all tasks that don't use the Anki collection.Related issues
Proposed changes
ankihub.gui.operations.AddonQueryOp
. This is a subclass ofaqt.operations.QueryOp
which is adapted for usage in the AnkiHub add-on.QueryOp
is a helper class to perform an operation on a background thread. It usesaqt.taskman.TaskManager.run_in_background
internally. See https://github.com/ankitects/anki/blob/ae6a03942f651790c40f8d8479f90eb7715bf2af/qt/aqt/operations/__init__.py#L167-L168. The main reason for creating this subclass, instead of just usingQueryOp
, is this:https://github.com/ankipalace/ankihub_addon/blob/4e5526aca819ca50eedf7987244804ceccff2a51/ankihub/gui/operations/__init__.py#L16-L27
aqt.mw.taskman.run_in_background
withAddonQueryOp
.For example this:
https://github.com/ankipalace/ankihub_addon/blob/798e5520bd97d30f33285f31054fb75b4355c51a/ankihub/gui/media_sync.py#L47-L50
gets replaced with this:
https://github.com/ankipalace/ankihub_addon/blob/4e5526aca819ca50eedf7987244804ceccff2a51/ankihub/gui/media_sync.py#L51-L55