[gnome-music/wip/mschraal/mediartloader-true-cancellation] cancellation meh
- From: Marinus Schraal <mschraal src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-music/wip/mschraal/mediartloader-true-cancellation] cancellation meh
- Date: Fri, 22 Apr 2022 14:02:16 +0000 (UTC)
commit 3b6ccadf96503383e585f28795e843f4a3f69e1b
Author: Marinus Schraal <mschraal gnome org>
Date: Fri Apr 22 11:09:14 2022 +0200
cancellation meh
gnomemusic/mediaartloader.py | 31 +++++++++++++++++++++----------
gnomemusic/texturecache.py | 16 +++++++++++++++-
2 files changed, 36 insertions(+), 11 deletions(-)
---
diff --git a/gnomemusic/mediaartloader.py b/gnomemusic/mediaartloader.py
index a5aca0f9a..e9c6b2b3b 100644
--- a/gnomemusic/mediaartloader.py
+++ b/gnomemusic/mediaartloader.py
@@ -44,11 +44,12 @@ class MediaArtLoader(GObject.GObject):
_log = MusicLogger()
- def __init__(self) -> None:
+ def __init__(self, cancellable: Gio.Cancellable) -> None:
"""Intialize MediaArtLoader
"""
super().__init__()
+ self._cancellable = cancellable
self._texture: Gdk.Texture
def start(self, uri: str) -> None:
@@ -60,7 +61,8 @@ class MediaArtLoader(GObject.GObject):
if thumb_file:
thumb_file.read_async(
- GLib.PRIORITY_DEFAULT_IDLE, None, self._open_stream, None)
+ GLib.PRIORITY_DEFAULT_IDLE, self._cancellable,
+ self._open_stream, None)
else:
self.emit("finished", None)
@@ -70,35 +72,44 @@ class MediaArtLoader(GObject.GObject):
try:
stream = thumb_file.read_finish(result)
except GLib.Error as error:
- self._log.warning(
- "Error: {}, {}".format(error.domain, error.message))
+ if error.code == Gio.IOErrorEnum.CANCELLED:
+ print("cancelled")
+ else:
+ self._log.warning(
+ "Error: {}, {}".format(error.domain, error.message))
self.emit("finished", None)
return
GdkPixbuf.Pixbuf.new_from_stream_async(
- stream, None, self._pixbuf_loaded)
+ stream, self._cancellable, self._pixbuf_loaded)
def _pixbuf_loaded(
self, stream: Gio.InputStream, result: Gio.AsyncResult) -> None:
try:
pixbuf = GdkPixbuf.Pixbuf.new_from_stream_finish(result)
except GLib.Error as error:
- self._log.warning(
- "Error: {}, {}".format(error.domain, error.message))
+ if error.code == Gio.IOErrorEnum.CANCELLED:
+ print("cancelled")
+ else:
+ self._log.warning(
+ "Error: {}, {}".format(error.domain, error.message))
self.emit("finished", None)
return
self._texture = Gdk.Texture.new_for_pixbuf(pixbuf)
stream.close_async(
- GLib.PRIORITY_DEFAULT_IDLE, None, self._close_stream)
+ GLib.PRIORITY_DEFAULT_IDLE, self._cancellable, self._close_stream)
def _close_stream(
self, stream: Gio.InputStream, result: Gio.AsyncResult) -> None:
try:
stream.close_finish(result)
except GLib.Error as error:
- self._log.warning(
- "Error: {}, {}".format(error.domain, error.message))
+ if error.code == Gio.IOErrorEnum.CANCELLED:
+ print("cancelled")
+ else:
+ self._log.warning(
+ "Error: {}, {}".format(error.domain, error.message))
self.emit("finished", self._texture)
diff --git a/gnomemusic/texturecache.py b/gnomemusic/texturecache.py
index 300f2e0ce..087e29b65 100644
--- a/gnomemusic/texturecache.py
+++ b/gnomemusic/texturecache.py
@@ -85,6 +85,7 @@ class TextureCache(GObject.GObject):
self._art_loader: MediaArtLoader
self._art_loading_id = 0
+ self._cancellable = Gio.Cancellable()
if TextureCache._cleanup_id == 0:
TextureCache._cleanup_id = GLib.timeout_add_seconds(
@@ -96,6 +97,7 @@ class TextureCache(GObject.GObject):
"""Disconnect ongoing lookup callback
"""
if self._art_loading_id != 0:
+ self._cancellable.cancel()
self._art_loader.disconnect(self._art_loading_id)
self._art_loading_id = 0
@@ -106,6 +108,11 @@ class TextureCache(GObject.GObject):
"""
self.clear_pending_lookup_callback()
+ if self._art_loading_id != 0:
+ self._art_loader.disconnect(self._art_loading_id)
+ self._art_loading_id = 0
+
+
if uri in TextureCache._textures.keys():
state, _, texture = TextureCache._textures[uri]
if state in [
@@ -115,7 +122,8 @@ class TextureCache(GObject.GObject):
TextureCache._textures[uri] = (state, time.time(), texture)
return
- self._art_loader = MediaArtLoader()
+ self._cancellable = Gio.Cancellable()
+ self._art_loader = MediaArtLoader(self._cancellable)
self._art_loading_id = self._art_loader.connect(
"finished", self._on_art_loading_finished, uri)
self._async_queue.queue(self._art_loader, uri)
@@ -136,6 +144,12 @@ class TextureCache(GObject.GObject):
"""Sorts the available cache entries by recency and evicts
the oldest items to match the maximum cache size.
"""
+ unavailable = {
+ k: (state, t, texture)
+ for k, (state, t, texture) in TextureCache._textures.items()
+ if state in [TextureCache.LoadingState.UNAVAILABLE]
+ }
+ print("UNAVAILABLE", unavailable)
sorted_available = {
k: (state, t, texture)
for k, (state, t, texture) in sorted(
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]