def on_resume(self, id): self.lock.acquire() isok = False flag = 0 try: index = find_request_by_id(id, self.pause_queue) request1 = self.pause_queue[int(index)] if len(self.serve_queue) < int( self.max_serve_num): # 如果服务队列不满,则直接加入服务队列 self.serve_queue.append(request1) self.serve_time_list.append(0) client = g_conn_pool_dict[id] client.sendall(RUNNINGMSG.encode('utf-8')) room_dict[id].request_state = 'running' flag = 1 else: locate = self.select_lowest_request() # 如果队列满,欺负最弱的 if request1.speed > self.serve_queue[locate].speed: self.swap_out(self.serve_queue[locate].id) self.serve_queue.append(request1) self.serve_time_list.append(0) client = g_conn_pool_dict[id] client.sendall(RUNNINGMSG.encode('utf-8')) room_dict[id].request_state = 'running' else: # 如果没有优先级更低的,进入等待队列 self.wait_queue.append(request1) self.wait_time_list.append(0) client = g_conn_pool_dict[id] client.sendall(WAITINGMSG.encode('utf-8')) room_dict[id].request_state = 'waiting' flag = 2 self.pause_queue.remove(request1) # 将结果写入数据库 now_time = get_sys_time() # sql = '' if flag == 1: sql = "insert into log values('" + now_time + "','" + id + "', 'running','" + str( request1.tem) + "','" + str(request1.speed) + "')" DB_Log = DBOperation.IMapper() DB_Log.update(sql) DB_Log.DB.close_conn() elif flag == 2: sql = "insert into log values('" + now_time + "','" + id + "', 'waiting','" + str( request1.tem) + "','" + str(request1.speed) + "')" DB_Log = DBOperation.IMapper() DB_Log.update(sql) DB_Log.DB.close_conn() finally: self.lock.release()
def set_pause(self): ac.handle_pause(self.id) room = room_dict[self.id] tem = room.ac_tem speed = room.ac_speed now_time = get_sys_time() sql = "insert into log values('" + now_time + "','" + self.id + "','pausing','" + str( tem) + "','" + str(speed) + "')" DB_Log = DBOperation.IMapper() DB_Log.update(sql) DB_Log.DB.close_conn()
def getStaff(): sql = "select username, passwd from staff" DB_Log = DBOperation.IMapper() result = DB_Log.query(sql) staff_dict = dict() for x in result: user = x[0] pwd = x[1] staff_dict[user] = pwd DB_Log.DB.close_conn() return staff_dict
def swap_out(self, id): self.lock.acquire() try: # 写入数据库 room = room_dict[id] now_time = get_sys_time() sql = "insert into log values('" + now_time + "','" + id + "', 'swap out','" + str( room.ac_tem) + "','" + str(room.ac_speed) + "')" DB_Log = DBOperation.IMapper() DB_Log.update(sql) DB_Log.DB.close_conn() result = find_request_by_id(id, self.serve_queue) self.wait_queue.append(self.serve_queue[result]) self.wait_time_list.append(0) self.serve_queue.remove(self.serve_queue[result]) self.serve_time_list.remove(self.serve_time_list[result]) client = g_conn_pool_dict[id] client.sendall(WAITINGMSG.encode('utf-8')) room_dict[id].request_state = 'waiting' finally: self.lock.release()
def add_request(self, request): self.lock.acquire() isok = False try: result = find_request_by_id(request.id, self.serve_queue) if result != FAILMSG: self.serve_queue.remove(self.serve_queue[result]) self.serve_time_list.remove(self.serve_time_list[result]) result = find_request_by_id(request.id, self.wait_queue) if result != FAILMSG: self.wait_queue.remove(self.wait_queue[result]) self.wait_time_list.remove(self.wait_time_list[result]) result = find_request_by_id(request.id, self.pause_queue) if result != FAILMSG: self.pause_queue.remove(self.pause_queue[result]) if len(self.serve_queue) < int(self.max_serve_num) and len( self.wait_queue) == 0: # 如果服务队列有空位,直接加 self.serve_queue.append(request) self.serve_time_list.append(0) client = g_conn_pool_dict[request.id] client.sendall(RUNNINGMSG.encode('utf-8')) room_dict[request.id].request_state = 'running' isok = True else: # 如果服务队列没空位,选择性的加 if request.speed != LOW: # 不是低风,存在直接进入运行队列的可能性 locate = self.select_lowest_request() if self.serve_queue[locate].speed < request.speed: self.swap_out(self.serve_queue[locate].id) self.serve_queue.append(request) self.serve_time_list.append(0) client = g_conn_pool_dict[request.id] client.sendall(RUNNINGMSG.encode('utf-8')) room_dict[request.id].request_state = 'running' isok = True else: self.wait_queue.append(request) # 直接进入等待队列 self.wait_time_list.append(0) client = g_conn_pool_dict[request.id] client.sendall(WAITINGMSG.encode('utf-8')) room_dict[request.id].request_state = 'waiting' elif request.speed == LOW: # 如果是低风 self.wait_queue.append(request) # 直接进入等待队列 self.wait_time_list.append(0) client = g_conn_pool_dict[request.id] client.sendall(WAITINGMSG.encode('utf-8')) room_dict[request.id].request_state = 'waiting' # 将结果写入数据库 now_time = get_sys_time() if isok: sql = "insert into log values('" + now_time + "','" + request.id + "', 'running','" + str( request.tem) + "','" + str(request.speed) + "')" else: sql = "insert into log values('" + now_time + "','" + request.id + "', 'waiting','" + str( request.tem) + "','" + str(request.speed) + "')" DB_Log = DBOperation.IMapper() DB_Log.update(sql) DB_Log.DB.close_conn() finally: self.lock.release()