Wrapped async in daemon thread for icon loading

This commit is contained in:
itdominator 2023-11-12 23:25:46 -06:00
parent 2de4de6b22
commit e6739c3087
3 changed files with 33 additions and 77 deletions

View File

@ -15,6 +15,7 @@ class AppLaunchException(Exception):
... ...
class Application(IPCServer): class Application(IPCServer):
""" docstring for Application. """ """ docstring for Application. """
@ -30,7 +31,7 @@ class Application(IPCServer):
message = f"FILE|{arg}" message = f"FILE|{arg}"
self.send_ipc_message(message) self.send_ipc_message(message)
raise AppLaunchException(f"{app_name} IPC Server Exists: Will send path(s) to it and close...") raise AppLaunchException(f"{app_name} IPC Server Exists: Have sent path(s) to it and closing...")
self.setup_debug_hook() self.setup_debug_hook()
Window(args, unknownargs) Window(args, unknownargs)

View File

@ -26,15 +26,14 @@ class GridMixin:
store.append([None, file[0]]) store.append([None, file[0]])
Gtk.main_iteration() Gtk.main_iteration()
# for i, file in enumerate(files): self.generate_icons(tab, store, dir, files)
# self.create_icon(i, tab, store, dir, file[0])
if use_generator: # NOTE: Not likely called often from here but it could be useful
# NOTE: tab > icon > _get_system_thumbnail_gtk_thread must not be used if save_state and not trace_debug:
# as the attempted promotion back to gtk threading stalls the generator. (We're already in main gtk thread) self.fm_controller.save_state()
for i, icon in enumerate( self.create_icons_generator(tab, dir, files) ):
self.load_icon(i, store, icon) @daemon_threaded
else: def generate_icons(self, tab, store, dir, files):
try: try:
loop = asyncio.get_running_loop() loop = asyncio.get_running_loop()
except RuntimeError: except RuntimeError:
@ -45,40 +44,18 @@ class GridMixin:
else: else:
asyncio.run( self.create_icons(tab, store, dir, files) ) asyncio.run( self.create_icons(tab, store, dir, files) )
# NOTE: Not likely called often from here but it could be useful
if save_state and not trace_debug:
self.fm_controller.save_state()
async def create_icons(self, tab, store, dir, files): async def create_icons(self, tab, store, dir, files):
tasks = [self.update_store(i, store, dir, tab, file[0]) for i, file in enumerate(files)] tasks = [self.update_store(i, store, dir, tab, file[0]) for i, file in enumerate(files)]
await asyncio.gather(*tasks) await asyncio.gather(*tasks)
async def load_icon(self, i, store, icon): async def load_icon(self, i, store, icon):
self.update_store(i, store, icon) GLib.idle_add(self.update_store, i, store, icon )
async def update_store(self, i, store, dir, tab, file): async def update_store(self, i, store, dir, tab, file):
icon = tab.create_icon(dir, file) icon = tab.create_icon(dir, file)
itr = store.get_iter(i) itr = store.get_iter(i)
store.set_value(itr, 0, icon) store.set_value(itr, 0, icon)
def create_icons_generator(self, tab, dir, files):
for file in files:
icon = tab.create_icon(dir, file[0])
yield icon
# @daemon_threaded
# def create_icon(self, i, tab, store, dir, file):
# icon = tab.create_icon(dir, file)
# GLib.idle_add(self.update_store, *(i, store, icon,))
#
# @daemon_threaded
# def load_icon(self, i, store, icon):
# GLib.idle_add(self.update_store, *(i, store, icon,))
# def update_store(self, i, store, icon):
# itr = store.get_iter(i)
# store.set_value(itr, 0, icon)
def create_tab_widget(self, tab): def create_tab_widget(self, tab):
return TabHeaderWidget(tab, self.close_tab) return TabHeaderWidget(tab, self.close_tab)

View File

@ -26,14 +26,14 @@ class GridMixin:
store.append([None, file[0]]) store.append([None, file[0]])
Gtk.main_iteration() Gtk.main_iteration()
if use_generator: self.generate_icons(tab, store, dir, files)
# NOTE: tab > icon > _get_system_thumbnail_gtk_thread must not be used
# as the attempted promotion back to gtk threading stalls the generator. (We're already in main gtk thread) # NOTE: Not likely called often from here but it could be useful
for i, icon in enumerate( self.create_icons_generator(tab, dir, files) ): if save_state and not trace_debug:
self.load_icon(i, store, icon) self.fm_controller.save_state()
else:
# for i, file in enumerate(files): @daemon_threaded
# self.create_icon(i, tab, store, dir, file[0]) def generate_icons(self, tab, store, dir, files):
try: try:
loop = asyncio.get_running_loop() loop = asyncio.get_running_loop()
except RuntimeError: except RuntimeError:
@ -44,40 +44,18 @@ class GridMixin:
else: else:
asyncio.run( self.create_icons(tab, store, dir, files) ) asyncio.run( self.create_icons(tab, store, dir, files) )
# NOTE: Not likely called often from here but it could be useful
if save_state and not trace_debug:
self.fm_controller.save_state()
async def create_icons(self, tab, store, dir, files): async def create_icons(self, tab, store, dir, files):
tasks = [self.update_store(i, store, dir, tab, file[0]) for i, file in enumerate(files)] tasks = [self.update_store(i, store, dir, tab, file[0]) for i, file in enumerate(files)]
await asyncio.gather(*tasks) await asyncio.gather(*tasks)
async def load_icon(self, i, store, icon): async def load_icon(self, i, store, icon):
self.update_store(i, store, icon) GLib.idle_add(self.update_store, i, store, icon )
async def update_store(self, i, store, dir, tab, file): async def update_store(self, i, store, dir, tab, file):
icon = tab.create_icon(dir, file) icon = tab.create_icon(dir, file)
itr = store.get_iter(i) itr = store.get_iter(i)
store.set_value(itr, 0, icon) store.set_value(itr, 0, icon)
def create_icons_generator(self, tab, dir, files):
for file in files:
icon = tab.create_icon(dir, file[0])
yield icon
# @daemon_threaded
# def create_icon(self, i, tab, store, dir, file):
# icon = tab.create_icon(dir, file)
# GLib.idle_add(self.update_store, *(i, store, icon,))
#
# @daemon_threaded
# def load_icon(self, i, store, icon):
# GLib.idle_add(self.update_store, *(i, store, icon,))
#
# def update_store(self, i, store, icon):
# itr = store.get_iter(i)
# store.set_value(itr, 0, icon)
def create_tab_widget(self, tab): def create_tab_widget(self, tab):
return TabHeaderWidget(tab, self.close_tab) return TabHeaderWidget(tab, self.close_tab)