Package horizons :: Package util :: Module preloader
[hide private]
[frames] | no frames]

Source Code for Module horizons.util.preloader

 1  # ################################################### 
 2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
 3  # team@unknown-horizons.org 
 4  # This file is part of Unknown Horizons. 
 5  # 
 6  # Unknown Horizons is free software; you can redistribute it and/or modify 
 7  # it under the terms of the GNU General Public License as published by 
 8  # the Free Software Foundation; either version 2 of the License, or 
 9  # (at your option) any later version. 
10  # 
11  # This program is distributed in the hope that it will be useful, 
12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
14  # GNU General Public License for more details. 
15  # 
16  # You should have received a copy of the GNU General Public License 
17  # along with this program; if not, write to the 
18  # Free Software Foundation, Inc., 
19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
20  # ################################################### 
21   
22  import logging 
23  import threading 
24   
25  from horizons.util.loaders.actionsetloader import ActionSetLoader 
26  from horizons.util.loaders.tilesetloader import TileSetLoader 
27  from horizons.util.python.callback import Callback 
28   
29  log = logging.getLogger("preload") 
30   
31   
32 -class PreloadingThread(threading.Thread):
33 """ 34 Preloads game data while in the main menu. Once a game is started, `wait_for_finish` is 35 called to end the thread. 36 """
37 - def __init__(self):
38 threading.Thread.__init__(self) 39 self.lock = threading.Lock()
40
41 - def run(self):
42 """ 43 Preloads game data. 44 45 Keeps releasing and acquiring lock, runs until lock can't be acquired. 46 """ 47 from horizons.entities import Entities 48 49 try: 50 # create own db reader instance, since it's not thread-safe 51 from horizons.main import _create_main_db 52 mydb = _create_main_db() 53 54 preload_functions = [ 55 ActionSetLoader.load, 56 TileSetLoader.load, 57 Callback(Entities.load_grounds, mydb, load_now=True), 58 Callback(Entities.load_buildings, mydb, load_now=True), 59 Callback(Entities.load_units, load_now=True) 60 ] 61 62 for f in preload_functions: 63 if not self.lock.acquire(False): 64 break 65 log.debug("Preload: %s", f) 66 f() 67 log.debug("Preload: %s is done", f) 68 self.lock.release() 69 log.debug("Preloading done.") 70 except Exception as e: 71 log.warning("Exception occurred in preloading thread: %s", e) 72 finally: 73 if self.lock.locked(): 74 self.lock.release()
75
76 - def wait_for_finish(self):
77 """ 78 Wait for preloading to finish. 79 """ 80 self.lock.acquire() 81 # wait until it finished its current action 82 if self.isAlive(): 83 self.join() 84 assert not self.isAlive() 85 else: 86 try: 87 self.lock.release() 88 except RuntimeError: 89 # due to timing issues, the lock might be released already 90 pass
91