Refactor async_call method into AsyncCall class

Provides more control over the created thread
This commit is contained in:
Xodetaetl 2015-08-30 16:38:11 +02:00
parent 6021f38d9b
commit d598c9767d
4 changed files with 28 additions and 22 deletions

View file

@ -11,7 +11,7 @@ from lutris.sync import Sync
from lutris.util import resources
from lutris.util.log import logger
from lutris.util.jobs import async_call
from lutris.util.jobs import AsyncCall
from lutris.util.strings import slugify
from lutris.util import datapath
@ -167,7 +167,7 @@ class LutrisWindow(object):
self.toggle_connection(False)
self.sync_library()
# Update Runtime
async_call(runtime.update_runtime, None, self.set_status)
AsyncCall(runtime.update_runtime, None, self.set_status)
@property
def current_view_type(self):
@ -258,13 +258,13 @@ class LutrisWindow(object):
self.view.set_uninstalled(game)
self.sidebar_treeview.update()
async_call(self.sync_icons, None)
AsyncCall(self.sync_icons, None)
self.set_status("Library synced")
GLib.idle_add(update_existing_games)
self.set_status("Syncing library")
sync = Sync()
async_call(sync.sync_all, update_gui)
AsyncCall(sync.sync_all, update_gui)
def sync_icons(self):
game_list = pga.get_games()
@ -349,7 +349,7 @@ class LutrisWindow(object):
def on_synchronize_manually(self, *args):
"""Callback when Synchronize Library is activated."""
self.sync_library()
async_call(runtime.update_runtime, None, self.set_status)
AsyncCall(runtime.update_runtime, None, self.set_status)
def on_resize(self, widget, *args):
self.window_size = widget.get_size()

View file

@ -15,7 +15,7 @@ from lutris import pga, settings
from lutris.runtime import get_runtime_env
from lutris.util import extract, devices, system
from lutris.util.fileio import EvilConfigParser, MultiOrderedDict
from lutris.util.jobs import async_call
from lutris.util.jobs import AsyncCall
from lutris.util.log import logger
from lutris.game import Game
@ -301,7 +301,7 @@ class ScriptInterpreter(object):
status_text = None
if status_text:
self.parent.set_status(status_text)
async_call(method, self._iter_commands, params)
AsyncCall(method, self._iter_commands, params)
else:
self._finish_install()
@ -725,7 +725,7 @@ class ScriptInterpreter(object):
appid
)
steam_runner.appid = appid
async_call(steam_runner.install_game, None, appid)
AsyncCall(steam_runner.install_game, None, appid)
else:
self._append_steam_data_to_files(runner_class)
@ -750,7 +750,7 @@ class ScriptInterpreter(object):
def complete_steam_install(self, dest):
winesteam_runner = winesteam.winesteam()
async_call(winesteam_runner.install, self.on_winesteam_installed, dest)
AsyncCall(winesteam_runner.install, self.on_winesteam_installed, dest)
def on_winesteam_installed(self, *args):
self.install_steam_game(winesteam.winesteam)

View file

@ -11,7 +11,7 @@ def download_asset(url, dest, overwrite=False):
if overwrite:
os.remove(dest)
else:
logger.info("Destination %s exists, not overwritting" % dest)
logger.info("Destination %s exists, not overwriting" % dest)
return
# TODO: Downloading icons and banners makes a bunch of useless http
# requests + it's really slow

View file

@ -4,23 +4,29 @@ from gi.repository import GLib
from lutris.util.log import logger
def async_call(func, on_done, *args, **kwargs):
""" Launch given function `func` in a new thread """
logger.debug("Async call: %s", str(func.__name__))
if not on_done:
on_done = lambda r, e: None
class AsyncCall(threading.Thread):
def __init__(self, function, on_done, *args, **kwargs):
"""Execute `function` in a new thread then schedule `on_done` for
execution in the main loop.
"""
super(AsyncCall, self).__init__(target=self.target, args=args,
kwargs=kwargs)
self.function = function
self.on_done = on_done if on_done else lambda r, e: None
self.daemon = kwargs.pop('daemon', False)
def do_call(*args, **kwargs):
self.start()
def target(self, *args, **kwargs):
result = None
error = None
try:
result = func(*args, **kwargs)
result = self.function(*args, **kwargs)
except Exception, err:
logger.error("Error while completing task %s: %s", func, err)
# raise # Uncomment this to inspect errors
logger.error("Error while completing task %s: %s",
self.function, err)
error = err
GLib.idle_add(lambda: on_done(result, error))
# raise # Uncomment this to inspect errors
thread = threading.Thread(target=do_call, args=args, kwargs=kwargs)
thread.start()
GLib.idle_add(lambda: self.on_done(result, error))