def __init__(self, task_loader, conf): self.lock = threading.Lock() self.workerManager = WorkerManager(conf) self.taskManager = TaskManager(task_loader) self.running_tasks = {} self.conf = conf self.load_tasks()
def lay_off_worker(self): if not PrintLists().print_workers(WorkerManager().get_worker_list()): return False id = PrintInputs.enter_worker_id( WorkerManager().get_worker_list()[-1].id) worker_remove = WorkerManager().remove_worker(id) if worker_remove: PrinterStatemants.print_worker_fired() return True else: PrinterStatemants.print_not_found(id, 'worker') return False
def edit_worker_salary(self): if not PrintLists().print_workers(WorkerManager().get_worker_list()): return False id = PrintInputs.enter_worker_id( WorkerManager().get_worker_list()[-1].id) salary = PrintInputs.enter_worker_new_salary() worker_edit = WorkerManager().edit_worker_salary(id, salary) if worker_edit: PrinterStatemants.print_worker_salary_updated() return True else: PrinterStatemants.print_not_found(id, 'worker') return False
def add_worker(self): worker = PrintInputs.enter_worker() if worker['age'] <= 18: print("We can't take him, he is too young") return False if worker['age'] >= 65: print("We can't take a pensioner") return False worker_add = WorkerManager().add_worker(worker) if worker_add: PrinterStatemants.print_worker_added() return True else: return False
def main(): # Logging logging.basicConfig(level=logging.DEBUG) logging.debug("Application starting...") # Read config config = ConfigParser() config.read("app.ini", encoding="UTF-8") mainConfig = MainConfig(config) logicConfig = LogicConfig(config) # Create components app = QApplication([]) engineCom = EngineCom() cabinCom = CabinCom() dashboard = Dashboard(mainConfig.initDriveMode) logic = Logic(logicConfig, mainConfig.initDriveMode) workerManager = WorkerManager( [engineCom.worker, cabinCom.worker, logic.worker]) tuning = Tuning(onExit=lambda: (workerManager.stopAll(), app.quit())) # Connect components engineCom.signals.outEngineTachometer.connect(logic.inEngineTachometer) engineCom.signals.outGearboxInTachometer.connect( logic.inGearboxInTachometer) engineCom.signals.outGearboxOutTachometer.connect( logic.inGearboxOutTachometer) engineCom.signals.outGearNumber.connect(logic.inGearNumber) engineCom.signals.outOilPressure.connect(logic.inOilPressure) engineCom.signals.outOilTemperature.connect(logic.inOilTemperature) engineCom.signals.outWatterTemperature.connect(logic.inWatterTemperature) cabinCom.signals.outRaceModeIsOn.connect(logic.inRaceModeIsOn) cabinCom.signals.outStopwatchButtonIsOn.connect( logic.inStopwatchButtonIsOn) cabinCom.signals.outOdometerButtonIsOn.connect(logic.inOdometerButtonIsOn) cabinCom.signals.outTurnLeftSignalIsOn.connect(logic.inTurnLeftSignalIsOn) cabinCom.signals.outTurnRightSignalIsOn.connect( logic.inTurnRightSignalIsOn) cabinCom.signals.outSteeringWhilePosition.connect( logic.inSteeringWhilePosition) cabinCom.signals.outAccelerometer.connect(logic.inAccelerometer) logic.signals.outMode.connect(dashboard.inMode) logic.signals.outTachometerEngine.connect(dashboard.inTachometerEngine) logic.signals.outTachometerGearbox.connect(dashboard.inTachometerGearbox) logic.signals.outTachometerGears.connect(dashboard.inTachometerGears) logic.signals.outAccelerometer.connect(dashboard.inAccelerometer) logic.signals.outSteeringWheelEncoder.connect( dashboard.inSteeringWheelEncoder) logic.signals.outTurnLeftIndicator.connect(dashboard.inTurnLeftIndicator) logic.signals.outTurnRightIndicator.connect(dashboard.inTurnRightIndicator) logic.signals.outSpeedometer.connect(dashboard.inSpeedometer) logic.signals.outGearNumber.connect(dashboard.inGearNumber) logic.signals.outOilWarningIndicator.connect( dashboard.inOilWarningIndicator) logic.signals.outWatterWarningIndicator.connect( dashboard.inWatterWarningIndicator) logic.signals.outSpeedometer.connect(dashboard.inSpeedometer) logic.signals.outStopwatch.connect(dashboard.inStopwatch) logic.signals.outOilManometer.connect(dashboard.inOilManometer) logic.signals.outOilThermometer.connect(dashboard.inOilThermometer) logic.signals.outWatterThermometer.connect(dashboard.inWatterThermometer) logic.signals.outOdometer.connect(dashboard.inOdometer) # Show UI if mainConfig.dashboardFullScreen: dashboard.showFullScreen() else: dashboard.show() if mainConfig.showTuningUi: tuning.show() # Run processing workerManager.startAll() # Start events loop sys.exit(app.exec_())
def trials(mu, theta2, lmbd, sigma2, EMitr, k, n, max_capital, num_agents, mode, err, alpha, trl): rng = np.random.RandomState(trl) idx = rng.randint(0, n, size=(k)) beta = np.zeros((n, 1)) beta[idx, :] = mu + np.sqrt(theta2) * rng.randn(k, 1) # #%% LATSI: func_class = LATSI(beta, mu, theta2, sigma2, lmbd, EMitr, err, num_agents, alpha, trl) worker_manager = WorkerManager(func_caller=func_class, worker_ids=num_agents, poll_time=1e-15, trialnum=trl) options = Namespace(max_num_steps=max_capital, num_init_evals=num_agents, num_workers=num_agents, mode=mode, GP=func_class) beta_hats = Bayesian_optimizer(worker_manager, func_class, options).optimize(max_capital) full_recovery_rate_latsi = [] partial_recovery_rate_latsi = [] for i in range(max_capital): beta_hat = beta_hats[i] est = (np.round(beta_hat) > (mu / 2)) real = (beta > 0) partial_recovery_rate_latsi.append(np.sum(est == real) / (n)) correct_LATSI = 0.0 if (np.all(est == real)): correct_LATSI = 1.0 full_recovery_rate_latsi.append(correct_LATSI) # # # #%% SPATS: func_class = SPATS(beta, mu, theta2, sigma2, lmbd, EMitr, num_agents, trl) worker_manager = WorkerManager(func_caller=func_class, worker_ids=num_agents, poll_time=1e-15, trialnum=trl) options = Namespace(max_num_steps=max_capital, num_init_evals=num_agents, num_workers=num_agents, mode=mode, GP=func_class) beta_hats = Bayesian_optimizer(worker_manager, func_class, options).optimize(max_capital) full_recovery_rate_spats = [] partial_recovery_rate_spats = [] for i in range(max_capital): beta_hat = beta_hats[i] est = (np.round(beta_hat) > (mu / 2)) real = (beta > 0) partial_recovery_rate_spats.append(np.sum(est == real) / (n)) correct_spats = 0.0 if (np.all(est == real)): correct_spats = 1.0 full_recovery_rate_spats.append(correct_spats) # #%% RSI: func_class2 = RSI(beta, mu, theta2, sigma2, lmbd, EMitr, err, trl) worker_manager = WorkerManager(func_caller=func_class2, worker_ids=num_agents, poll_time=1e-15, trialnum=trl) options = Namespace(max_num_steps=max_capital, num_init_evals=num_agents, num_workers=num_agents, mode=mode, GP=func_class2) beta_hats = Bayesian_optimizer(worker_manager, func_class2, options).optimize(max_capital) full_recovery_rate_rsi = [] partial_recovery_rate_rsi = [] for i in range(max_capital): beta_hat = beta_hats[i] est = (np.round(beta_hat) > (mu / 2)) real = (beta > 0) partial_recovery_rate_rsi.append(np.sum(est == real) / (n)) correct_rsi = 0.0 if (np.all(est == real)): correct_rsi = 1.0 full_recovery_rate_rsi.append(correct_rsi) return [ full_recovery_rate_spats, full_recovery_rate_rsi, full_recovery_rate_latsi, partial_recovery_rate_spats, partial_recovery_rate_rsi, partial_recovery_rate_latsi ]
class Master(object): def __init__(self, task_loader, conf): self.lock = threading.Lock() self.workerManager = WorkerManager(conf) self.taskManager = TaskManager(task_loader) self.running_tasks = {} self.conf = conf self.load_tasks() def get_status(self): return { 'total_workers': self.workerManager.get_workers(), 'tasks': self.taskManager.get_tasks_stats(), 'idle_workers': self.workerManager.get_idle_workers() } def clean_death_workers(self): '''定期检查worker的心跳信息,及时清除死亡worker''' workers, tasks = self.workerManager.clean_death_workers() logging.info("death workers:%s; relatedTasks:%s", workers, tasks) for task in tasks: self.taskManager.fail_task(task.uuid, TaskStatus.notReturned) return workers def register_worker(self, worker): '''注册作业节点''' logging.info("%s come in", worker) status = "OK" if worker is not None: self.workerManager.add_worker(worker) else: status = "Invalid" # logging.info(self.workerManager.get_workers()) return status def remove_worker(self, worker): status = "OK" if worker is None: status = "Invalid" return status identifier = worker.get_uuid() w, tasks = self.workerManager.remove_worker(identifier) for task in tasks: self.taskManager.fail_task(task.get_uuid(), TaskStatus.notReturned) if w is None: status = "NOT EXISTS" return status def task_complete(self, worker, taskResult): '''worker完成一个作业,返回作业统计信息,worker重新归于队列''' self.workerManager.finish_task(worker, taskResult) self.workerManager.update_worker(worker) if taskResult.is_success(): self.taskManager.finish_task(taskResult.get_task_uuid()) else: self.taskManager.fail_task(taskResult.get_task_uuid(), TaskStatus.failedToExecute) return True def heartbeat(self, worker): '''收到心跳信息,更新该工作节点的信息''' self.workerManager.update_worker(worker) return True def lookup_spider(self, spider): pass def load_tasks(self): self.taskManager.load_tasks() def schedule_next(self): logging.info('tasks: %s', self.taskManager.get_tasks_stats()) task = self.taskManager.next_task() worker = self.workerManager.next_worker() self.workerManager.assign_task(worker, task) try: proxy = common.RPCServerProxy.get_proxy(worker) proxy.assign_task(task) except Exception, e: traceback.print_exc() self.remove_worker(worker)
import fire from config import cfg from tasks import * from worker_manager import WorkerManager import time wm = WorkerManager() def print_task(): ''' 暂时没啥用,留了接口 ''' wm.show_occupied() def start(): wm.occupy_start() if __name__ == '__main__': fire.Fire()
def get_total_salary(self): total_salary = 0 workers = WorkerManager().get_worker_list() for worker in workers: total_salary += worker.salary return total_salary
class Master(object): def __init__(self, task_loader, conf): self.lock = threading.Lock() self.workerManager = WorkerManager(conf) self.taskManager = TaskManager(task_loader) self.running_tasks = {} self.conf = conf self.load_tasks() def get_status(self): return { 'total_workers': self.workerManager.get_workers(), 'tasks': self.taskManager.get_tasks_stats(), 'idle_workers': self.workerManager.get_idle_workers() } def clean_death_workers(self): '''定期检查worker的心跳信息,及时清除死亡worker''' workers,tasks = self.workerManager.clean_death_workers() logging.info("death workers:%s; relatedTasks:%s", workers, tasks) for task in tasks: self.taskManager.fail_task(task.uuid, TaskStatus.notReturned) return workers def register_worker(self, worker): '''注册作业节点''' logging.info("%s come in", worker) status = "OK" if worker is not None: self.workerManager.add_worker(worker) else: status = "Invalid" # logging.info(self.workerManager.get_workers()) return status def remove_worker(self, worker): status = "OK" if worker is None: status = "Invalid" return status identifier = worker.get_uuid() w, tasks = self.workerManager.remove_worker(identifier) for task in tasks: self.taskManager.fail_task(task.get_uuid(), TaskStatus.notReturned) if w is None: status = "NOT EXISTS" return status def task_complete(self, worker, taskResult): '''worker完成一个作业,返回作业统计信息,worker重新归于队列''' self.workerManager.finish_task(worker, taskResult) self.workerManager.update_worker(worker) if taskResult.is_success(): self.taskManager.finish_task(taskResult.get_task_uuid()) else: self.taskManager.fail_task(taskResult.get_task_uuid(), TaskStatus.failedToExecute) return True def heartbeat(self, worker): '''收到心跳信息,更新该工作节点的信息''' self.workerManager.update_worker(worker) return True def lookup_spider(self, spider): pass def load_tasks(self): self.taskManager.load_tasks() def schedule_next(self): logging.info('tasks: %s',self.taskManager.get_tasks_stats()) task = self.taskManager.next_task() worker = self.workerManager.next_worker() self.workerManager.assign_task(worker, task) try: proxy = common.RPCServerProxy.get_proxy(worker) proxy.assign_task(task) except Exception,e: traceback.print_exc() self.remove_worker(worker)