def handle_failed_result(self, task_id): """save the failed status result Args: task_id (str): the result under this task_id """ from job_manager import JobManager logger = Logger().get() logger.debug( f"save_result: task_id: {task_id} status: {Status.Failed}") ResourceManager().save_result(task_id, []) ResourceManager().update_task_status(task_id, Status.Failed) JobManager().finish_task(task_id) try: container = self.id_to_task_container[task_id][1] except Exception as e: logger.info( f"exception for finding container correspoding to {task_id}, status:{Status.Failed}, maybe the container is forced killed before, {e}" ) else: self.id_to_task_container.pop(task_id, None) container.stop() container.remove() logger.debug( f"for task:{task_id}, can't run cmd normally, exit normally")
def get_stderr(task_id): print(task_id) stderr = ResourceManager().get_stderr(task_id) if stderr is None: abort(404) else: status_str = ResourceManager().get_status(task_id) return {"Status": status_str, "Content": stderr}
def setup_pharos_tools(app): # Remove the check for non-debug mode # It means "Only run when app has been loaded" # Flask will run it twice to enable the "reload" feature in debug mode is_initialized = ResourceManager().setup() if is_initialized == False: ResourceManager().initialize_pharos_tools() t = Thread(target=thread_update_kernel) t.start()
def submit_job(): """Test command: curl -H "Content-Type: application/json" --request POST -d @/vagrant/tests/sample_output/input.json http://localhost:5000/job" """ if ResourceManager().get_updating_kernel() is True: return { "Status": "Currently the Pharos toolset is updating. Try later please." } request_json = request.get_json() # print(request.get_json()) job = ResourceManager().save_new_job_and_tasks(request_json) thread = Thread(target=submit_job_through_job_manager, args=(job, )) thread.start() return {"Status": "ok"}
def test_bindings_clash(): with pytest.raises(SemanticError): ResourceManager().string_input(''' def f(x): x = 1 return 2 ''')
def test_string_input(): rm = ResourceManager() rm.string_input(''' a = [1,2,3] b = sum(a) ''') assert rm.b == 6
def main(): # change the current directory to the one of the game # this is to allow executions like ``python src/tct.py'' try: os.chdir(os.path.abspath(os.path.dirname(sys.argv[0]))) except IOError as e: print(e) exit(constants.FILE_ERR) # create the resource manager and initialize it. resourceManager = ResourceManager() resourceManager.setResourcesPath(constants.RESOURCES_DIR) resourceManager.setImagesPath(constants.GRAPHICS_DIR) # get the command line options; return the option flags game_opts = get_parsed_opts() # MVC stuff event_manager = EventManager() gui_view = MainGUIView(event_manager, game_opts) # the game manager game_manager = GameManager(game_opts) # controller which handles the main game loop main_controller = MainController(event_manager, gui_view, game_manager) # keep running the game until a quit event occurs main_controller.run() # if control somehow reaches this point close all the pygame subsystems pygame.quit() safe_exit()
def __init__(self): pygame.init() self.clock = pygame.time.Clock() self.fps = SETTINGS['default_fps'] # fullscreen on/off if SETTINGS['fullscreen_mode']: self.screen = pygame.display.set_mode(( SETTINGS['window_x_size'], SETTINGS['window_y_size'] ), pygame.FULLSCREEN ) else: self.screen = pygame.display.set_mode(( SETTINGS['window_x_size'], SETTINGS['window_y_size'] )) # load resource manager self.rm = ResourceManager() # load input manager self.im = InputManager() # state setup self.cur_state = BoardState(self.screen,self.rm) # gui self.gui = PGui(self)
def kill_job(self, job_id): """Kill all unfinished tasks of this job. Args: job_id (String): The id of the job """ logger = Logger().get() logger.debug(f"Start killing the job({job_id})") try: job = self.job_metadata[job_id] if job.finished_count == len(job.tasks): self.job_metadata.pop(job_id, None) logger.debug(f"Job({job_id}) has finished. Skipped killing.") return killed_count = 0 for task in job.tasks: if job.finished_map[task.task_id] == False: ExecutionManager().kill_task(task.task_id) killed_count += 1 ResourceManager(self.db_name).update_job_status( job_id, Status.Killed) self.job_metadata.pop(job_id, None) logger.debug(f"Killed {killed_count} tasks of Job({job_id}).") except Exception as e: logger.error( f"Something wrong when killing job({job_id}). Exception: {e}")
def save_realtime_stderr(): Thread(target=lambda task_id, stderr: ResourceManager().update_stderr( task_id, stderr), args=( request.form['task_id'], request.form['stderr'], )).start() return {"status": "ok"}
def __init__(self, kvstore_host): log.info("Initializing KeyValueStoreClient") self.rm = ResourceManager() self.KV_STORE_HOST = kvstore_host self.KV_STORE_DB_PATH = 'mysql+pymysql://root:P@ssword123@{}:3306/kvstore'.format( kvstore_host) self.db = DataBaseHandler(self.KV_STORE_DB_PATH) log.info("Initializing KeyValueStoreClient is successful")
def __init__(self, root): self.resource_manager = ResourceManager(Bank(self)) self.wave_count = 1 self._root = root self._gameRunning = False self._currWaveNumber = 0 self._currWave = None self._canv = Canvas(root, width=CANVAS_DIM, height=CANVAS_DIM) self._bottom_panel = Frame(root) self._bottom_panel.pack() self._canv.pack() self._btStartGame = Button(self._bottom_panel, text="Start Game", command=self.startGame) self._btStartGame.pack(side=LEFT) Label(self._bottom_panel, text="Gold: ").pack(side=LEFT) self._goldAmtVar = IntVar() self._goldAmtVar.set(self.resource_manager.getMoney()) self._goldLbl = Label(self._bottom_panel, textvariable=self._goldAmtVar) self._goldLbl.pack(side=LEFT) self._btNextWave = Button(self._bottom_panel, text="Start Wave", command=self.startNextWave) self._btNextWave.pack(side=LEFT) Label(self._bottom_panel, text="Time till next wave starts: ").pack(side=LEFT) self._timeLeftTilWave = IntVar() self._timeLeftTilWave.set(TIME_BETWEEN_WAVES) self._timeLeftLbl = Label(self._bottom_panel, textvariable=self._timeLeftTilWave) self._timeLeftLbl.pack(side=LEFT) # A 2-d grid of locations self._grid = [] for row in range(NUM_CELLS_PER_DIM): rowlist = [] for col in range(NUM_CELLS_PER_DIM): cell = Cell(self._canv, col, row, SQUARE_SIZE, self.resource_manager) rowlist.append(cell) self._grid.append(rowlist) # Follow the mouse everywhere and highlight the cell it is in. # self._canv.bind("<Motion>", self.highlight_cell) # Read path info from a file and highlight the path on the screen. self.readPathInfo() self.wave = Wave(self.resource_manager, self._canv, self._path)
def _sliceSpriteSheet (self, sheet, width, height): images = [] masterImage = ResourceManager().getImage(sheet, self._reuse) masterWidth, masterHeight = masterImage.get_size () for i in range (int (masterWidth / width)): images.append (masterImage.subsurface ((i * width, 0, width, height))) return images
def __init__(self, *args, **kwargs): log.info("Initializing MapReduceMaster") self.rm = ResourceManager() self.KV_STORE_HOST = self.rm.find_kvstore() self.kvstore = KeyValueStoreClient(self.KV_STORE_HOST) self.workers = Queue() self.workers_mutex = threading.Lock() log.info("Initializing MapReduceMaster is complete, KV_STORE_HOST: {}". format(self.KV_STORE_HOST))
def __init__(self, game_opts, *menu_opts): # create a programmer friendly mapping stucture of the menu options self.options = [{'label': x[0], 'callable': x[1]} for x in menu_opts] self.game_opts = game_opts # set the default menu dimensions (these dimensions # are calculated depending on the font and its size) self.width = 0 self.height = 0 # set up the default coordinates of the menu self.x = 0 self.y = 0 # the topleft corner of the screen self.screen_topleft_offset = (0, 0) # set the default focused option self.option = 0 # set the default previous focused option self.option_previous = self.option # set the default normal color of the menu font self.normal_color = pygame.Color('black') # set the default focused color of the menu font self.focus_color = pygame.Color('red') # default is to enable support of menu image on focused options self.image_enabled = True # default is to enable support mouse on menu self.mouse_enabled = True # set mouse focusing at unknown by default self.mouse_focus = None self._font = None # set a default font and its size (also fix size) self.font = pygame.font.Font(None, FONT_SIZE) self._fix_size() # set the default sound to play when an option is focused self.focus_sound = load_sound( constants.FILES['sounds']['menu']['share']['focus'][0]) # set the default sound to play when an option is entered self.select_sound = load_sound( constants.FILES['sounds']['menu']['share']['sel'][0]) # set the default graphic to display before the option label self.focus_graphic = ResourceManager().getImage( constants.FILES['graphics']['menu']['share']['focus'][0])
def get_job(id): """Test command: curl --request GET http://localhost:5000/job-info/<id>/json Test steps: 1. Modify line 14 & 15 of this file to use database "test" 2. Run "curl --request GET http://localhost:5000/clean-all" 3. Submit jobs through browser 4. Run "curl --request GET http://localhost:5000/job-list/json" to get the list 5. Run "curl --request GET http://localhost:5000/job-info/<id>/json" where the id is of the first job in the list 6. Run "curl --request GET http://localhost:5000/clean-all" after use 7. Remember to modify the name of the database """ job_dict = ResourceManager().get_job_info(id) return job_dict
def initialize_job(self, new_job): """Initialize additional metadata of the new job. Args: new_job (Job): The job object """ new_job.finished_count = 0 new_job.finished_map = {} for task in new_job.tasks: new_job.finished_map[task.task_id] = False self.job_metadata[new_job.job_id] = new_job for task in new_job.tasks: self.task_id_to_job_id[task.task_id] = new_job.job_id ResourceManager(self.db_name).update_job_status( new_job.job_id, Status.Running)
def update_kernel(): """update backend Pharos tool from docker hub The form of the POST format: {Content: seipharos/pharos:latest} Test command: curl http://localhost:5000/pharos -X POST -d "Content=seipharos/pharos:latest" curl http://localhost:5000/pharos -X POST -d "Content=ubuntu" """ if ResourceManager().get_updating_kernel() is True: return { "Status": "Currently the Pharos toolset is updating. Try later please." } BASE_IMAGE = request.get_json()['Content'] t = Thread(target=thread_update_kernel, args=(BASE_IMAGE, )) t.start() return {"Status": "ok"}
def __init__(self, manager, slides): self.event_manager = manager self.event_manager.register_listener(self) self.screen = pygame.display.get_surface() if not len(slides): raise SlideLenError('Incorrect slide length: {0}'.format(sl)) self.slides = slides parser = ConfigParser(constants.MAIN_CFG, constants.CFG_XMLNS) dir_name = parser.first_match('intro').attrib file_path = path.join(dir_name['dir'], parser.first_match('blank').text) self.blank = ResourceManager().getImage(file_path) self.alphavalue = MAX_ALPHA
def __init__(self): try: os.environ['SDL_VIDEO_WINDOW_POS'] = str(44) + "," + str(44) self.commandQueue_l = [] self.projectionMatrix = None self.configs = Configs() self.globalStates = GlobalStates() self.commander = Commander(self) self.globalStates.winWidth_i = self.configs.initScreenSizeWidth_i self.globalStates.winHeight_i = self.configs.initScreenSizeHeight_i p.init() winSize_t = ( self.globalStates.winWidth_i, self.globalStates.winHeight_i ) if self.configs.fullScreen_b: p.display.set_mode(winSize_t, pl.DOUBLEBUF | pl.OPENGL | pl.FULLSCREEN) else: p.display.set_mode(winSize_t, pl.DOUBLEBUF | pl.OPENGL | pl.RESIZABLE) self.player = Player() self.camera = Camera("main_camera", self.player, (0.0, 1.6, 0.0)) self.controller = Controller(self, self.player) self.resourceManager = ResourceManager(self.globalStates) self.resourceManager.runProcesses() self.resourceManager.requestLevelLoad("entry", 5.0) self.console = self.resourceManager.overlayUiMan.consoleWin self.fMan = FrameManager( False, self.resourceManager.overlayUiMan.frameDisplay.update, 0 ) self.renderDude = RenderingDude(self.player, self.resourceManager, self.camera, self.configs) self.logicalDude = LogicalDude(self.resourceManager, self.player, self.commandQueue_l, self.globalStates) self.globalStates.menuPopUp_b = True self.controller.enableMouseControl(False) self.initGL() except: self.terminate() raise
def __init__(self, hyper_params, max_num=9999, random=True): self.hp_name = list(hyper_params.keys()) self.hp = self.get_hp(hyper_params) self.max_num = max_num self.random = random self.resource_manager = ResourceManager(mem_limit=1, cpu_limit=0.1, gpu_limit=0.5, max_instances=self.max_num) self.waiting_pool = [item for item in self.hp] self.finished_pool = [] self.working_pool = [] self.working_process = [] self.lock_list = [] self.shared_eps_num_list = [] self.shared_eps_reward_list = []
def __init__(self, manager, game_opts): self.event_manager = manager self.event_manager.register_listener(self) self.game_opts = game_opts GAME_WINDOW_TITLE = constants.GAME_PACKAGE # make the window of the game always centered os.environ["SDL_VIDEO_CENTERED"] = "1" pygame.init() pygame.display.set_mode( (constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT), pygame.FULLSCREEN if self.game_opts.fullscreen else 0) pygame.display.set_caption(GAME_WINDOW_TITLE) pygame.display.set_icon(ResourceManager().getImage( constants.FILES['graphics']['window']['icon'][0])) pygame.mouse.set_visible(False)
async def main(): cache_manager = CacheManager() truly_awesome_bank_API_client = TrulyAwesomeBankAPIClient() resource_manager = ResourceManager(cache_manager, truly_awesome_bank_API_client) transaction = {'type': 'PAYMENT', 'amount': 100, 'currency': 'EUR'} print('=======================') print('| WRITE THROUGH |') print('=======================') print('>>> Save transaction') entry = await resource_manager.save_with_write_through(transaction) print('>>> Get transaction') await resource_manager.fetch_transaction_by_id(entry['id']) print('=======================') print('| WRITE BEHIND |') print('=======================') print('>>> Save transaction') entry = await resource_manager.save_with_write_behind(transaction) print('>>> Get transaction') await resource_manager.fetch_transaction_by_id(entry['id']) print('') print('--------------------------------------------') print('| AWESOME BANK DATABASE (before sync) |') print('--------------------------------------------') print(truly_awesome_bank_API_client._TrulyAwesomeBankAPIClient__database) print('') # wait for synchronization await asyncio.sleep(10) print('') print('--------------------------------------------') print('| AWESOME BANK DATABASE (after sync) |') print('--------------------------------------------') print(truly_awesome_bank_API_client._TrulyAwesomeBankAPIClient__database) print('')
def submit_task(self, task): """Job Manager call this method and submit a task. This method will launch the whole procedure (i.e. parse cmd, create container, run exe with phraos and insert result into Resource Manager) of the Execution Manager Args: task (Task): The task from Job Manager Returns: if successful, return true to Job Manager """ logger = Logger().get() logger.debug( f"receive task: task_id = {task.task_id}, job_id = {task.job_id}") self.set_attr( task ) #this will change task.arguments to the path corresponding to the path inside the container client = docker.from_env() container = client.containers.create( "felucca/pharos", command="/bin/bash", environment=[f"TASK_ID={task.task_id}"], tty=True, stdin_open=True, auto_remove=False) logger.debug(f"successfully create a container : {container.name}") self.set_map(task, container) container.start() self.copy_to_container(task, container) logger.debug(f"successfully copy exe into container({container.name})") ResourceManager().update_task_status(task.task_id, Status.Running) t = Thread(target=self.run_container_flask, args=(container, )) t.start() #self.copy_to_container(task,task.executable_file,container) return (True)
def finish_job(self, job_id): """Set the job status to finished and clean its metadata. Args: job_id (String): The id of the job """ logger = Logger().get() try: ResourceManager(self.db_name).mark_job_as_finished(job_id) job = self.job_metadata[job_id] for task in job.tasks: self.task_id_to_job_id.pop(task.task_id) self.job_metadata.pop(job_id) logger.debug(f"Job({job_id}) is removed.") except Exception as e: logger.error( f"Something wrong in finishing job({job_id}). Exception: {e}")
def kill_task(self, task_id): """try to kill and remove the container correspoding to the given task_id, if succeed, update the status at RM Args: task_id (Task): The id of the task """ logger = Logger().get() try: container = self.id_to_task_container[task_id][1] container.stop() container.remove() self.id_to_task_container.pop(task_id, None) ResourceManager().update_task_status(task_id, Status.Killed) from job_manager import JobManager JobManager().finish_task(task_id) except Exception as e: logger.error( f"try to kill {task_id}'s container fail, maybe the container is not existed or already killed, exception: {e}" )
def __init__(self, game_opts): State.__init__(self, constants.SCENES['intro']) self.game_opts = game_opts parser = ConfigParser(constants.MAIN_CFG, constants.CFG_XMLNS) # intro slides dir_name = parser.first_match('intro').attrib slides = [i.text for i in parser.all_matches('slide')] slides = [path.join(dir_name['dir'], i) for i in slides] slide_num = len(slides) self.slides = [ResourceManager().getImage(slides[i]) for i in range(slide_num)] self.cutscenes = IntroCutScene(self.slides) pygame.mixer.music.set_volume(0.0) sound_mixer.play_music( constants.FILES['sounds']['menu']['share']['bg'][0]) if self.game_opts.music: pygame.mixer.music.unpause() else: pygame.mixer.music.pause() pygame.mixer.music.set_volume(MAX_VOLUME)
def __init__(self, analysis_config, train_configs, datasets, split_names, base_models=[], output_dir=None, search_name=None, monitor_cpu=True, monitor_gpu=True, cpu_cores=[], gpu_devices=[]): self._analysis_cfg = analysis_config # create trial output dir if not specified if search_name is None: search_name = 'gqcnn_hyperparam_search_{}'.format(gen_timestamp()) if output_dir is None: output_dir = 'models' self._trial_output_dir = os.path.join(output_dir, search_name) if not os.path.exists(self._trial_output_dir): os.makedirs(self._trial_output_dir) # set up logger self._logger = Logger.get_logger(self.__class__.__name__, log_file=os.path.join(self._trial_output_dir, 'search.log'), global_log_file=True) # init resource manager self._resource_manager = ResourceManager(TrialConstants.TRIAL_CPU_LOAD, TrialConstants.TRIAL_GPU_LOAD, TrialConstants.TRIAL_GPU_MEM, monitor_cpu=monitor_cpu, monitor_gpu=monitor_gpu, cpu_cores=cpu_cores, gpu_devices=gpu_devices) # parse train configs and generate individual trial parameters if len(base_models) > 0: assert len(train_configs) == len(datasets) == len(split_names) == len(base_models), 'Must have equal number of training configs, datasets, split_names, and base models!' else: assert len(train_configs) == len(datasets) == len(split_names), 'Must have equal number of training configs, datasets, and split_names!' self._logger.info('Generating trial parameters...') trial_params = gen_trial_params(train_configs, datasets, split_names, base_models=base_models) # create pending trial queue self._trials_pending_queue = Queue() if len(base_models) > 0: for trial_name, hyperparam_summary, train_cfg, dataset, base_model, split_name in trial_params: self._trials_pending_queue.put(GQCNNFineTuningAndAnalysisTrial(self._analysis_cfg, train_cfg, dataset, base_model, split_name, self._trial_output_dir, trial_name, hyperparam_summary)) else: for trial_name, hyperparam_summary, train_cfg, dataset, split_name in trial_params: self._trials_pending_queue.put(GQCNNTrainingAndAnalysisTrial(self._analysis_cfg, train_cfg, dataset, split_name, self._trial_output_dir, trial_name, hyperparam_summary)) # create containers to hold running, finished, and errored-out trials self._trials_running = [] self._trials_finished = [] self._trials_errored = []
from resource_manager import ResourceManager r = ResourceManager(90, 90, 'B')
def main(): pygame.init() SCREEN_SIZE = (800, 600) screen = pygame.display.set_mode(SCREEN_SIZE) area = screen.get_rect() half = area.inflate((-SCREEN_SIZE[0] / 2., -SCREEN_SIZE[1] / 2.)) resourceManager = ResourceManager() resourceManager.setResourcesPath("resources") resourceManager.setImagesPath("graphics") defaultLimiter = LimiterFactory().getInstance('Default') sprites = [] sprites.append( ShakingGO("sprite/sprite-1.png", (area.center), random.randint(0, 30), random.randint(20, 255), False, random.randint(50, 150), area)) sprites.append( ShakingGO("sprite/sprite-2.png", (area.center), random.randint(0, 30), random.randint(20, 255), False, random.randint(50, 150), area)) sprites.append( CardinalGO("sprite/sprite-3.png", (area.center), random.randint(0, 30), random.randint(20, 255), False, random.randint(50, 150), area, 'West')) sprites.append( CardinalGO("sprite/sprite-4.png", (area.center), random.randint(0, 30), random.randint(20, 255), False, random.randint(50, 150), area, 'North')) sprites.append( HipparchusGO("sprite/sprite-5.png", (area.center), random.randint(0, 30), random.randint(20, 255), False, random.randint(50, 150), area, 'Random')) sprites.append( HipparchusGO("sprite/sprite-6.png", (area.center), random.randint(0, 30), random.randint(20, 255), False, random.randint(50, 150), area, 'Random')) sprites.append( StaticGO("sprite/sprite-7.png", (area.center), random.randint(0, 30), random.randint(20, 255), False)) hipparchusGO = HipparchusGO("sprite/sprite-9.png", (half.center), random.randint(0, 30), random.randint(20, 255), False, random.randint(50, 150), half, 'Random') shakingGO = ShakingGO("sprite/sprite-9.png", (half.center), random.randint(0, 30), random.randint(20, 255), True, random.randint(50, 150), half) travelGO = TravelGO("sprite/sprite-1.png", (half.center), random.randint(0, 30), random.randint(20, 255), False, random.randint(50, 150), half) sprites.append(hipparchusGO) sprites.append(shakingGO) sprites.append(travelGO) for sprite in sprites: sprite.limiter = defaultLimiter explosionGO = ExplosionGO("sprite/explosion.png", (area.center), random.randint(0, 30), random.randint(20, 255), False, 20, 20, 10) runningGO = RunningGO("sprite/character-hand.png", (area.center), random.randint(0, 30), random.randint(20, 255), False, 50, 72, 10) sprites.append(explosionGO) sprites.append(runningGO) group = pygame.sprite.LayeredUpdates((sprites)) background = ResourceManager().getImage("sprite/background.jpg") screen.blit(background, (0, 0)) pygame.display.update() clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() elif event.type == pygame.MOUSEMOTION: travelGO.destination = pygame.mouse.get_pos() elif event.type == pygame.MOUSEBUTTONDOWN: shakingGO.position = pygame.mouse.get_pos() shakingGO._layer = random.choice(group.layers()) shakingGO.speed = random.randint(50, 150) shakingGO.alpha = random.randint(20, 255) travelGO.move() elif event.type == pygame.MOUSEBUTTONUP: travelGO.still() elif event.type == pygame.KEYDOWN: hipparchusGO.position = pygame.mouse.get_pos() hipparchusGO._layer = random.choice(group.layers()) hipparchusGO.speed = random.randint(50, 150) runningGO.position = pygame.mouse.get_pos() explosionGO._layer = random.choice(group.layers()) explosionGO.alpha = random.randint(20, 255) time_passed_seconds = clock.tick() / 1000.0 print clock.get_fps() group.update(time_passed_seconds) group.clear(screen, background) changes = group.draw(screen) pygame.display.update(changes)