下面的代码哪里写的有错误，一直卡住
async def rebalance_request(self):
        last_rebalance_time = time.time()
        
        while True:
            try:
                now = time.time()

                need_rebalance = False            
                slack_map = {}

                for decode_index in self.d_set:
                    try:
                        engine = self.pd_engine[decode_index]
                        scheduler = engine.scheduler[0]
                        tpot_predict = self.pd_profiler.get_decode_seq_prediction(scheduler.running)

                        slack_list = []

                        for seq_group in scheduler.running:
                            decode_slack = self.pd_profiler.get_deocode_slack_on_pipeline(seq_group, now, tpot_predict)
                            slack_list.append((seq_group, decode_slack))

                        sorted_slack_list = sorted(slack_list, key=lambda x: x[1], reverse=False)

                        slack_map[decode_index] = sorted_slack_list

                    except Exception as e:
                        print(f"Error processing decode_index {decode_index}: {e}")
                        continue

                # if two instances don't have running 
                if (len(slack_map.get(2, [])) == 0 and len(slack_map.get(3, [])) == 0):
                    need_rebalance = False
                    continue

                print(f"************************* slack_map:{slack_map}")

                try:
                    instance0_min_slack = slack_map[2][0][1]
                    instance0_max_slack = slack_map[2][-1][1]
                    instance1_min_slack = slack_map[3][0][1]
                    instance1_max_slack = slack_map[3][-1][1]
                except IndexError:
                    print("IndexError: One of the instances has no running tasks.")
                    continue

                if (instance0_min_slack > 0.5) or (instance1_min_slack > 0.5):
                    need_rebalance = False
                    continue

                slow_instance_id = None
                fast_instance_id = None

                # tag slow instance and fast instance
                if len(slack_map.get(2, [])) == 0:
                    if (instance1_max_slack > 2) and (instance1_min_slack < 0.5):
                        slow_instance_id = 2
                        fast_instance_id = 3
                        need_rebalance = True
                    else:
                        need_rebalance = False
                elif len(slack_map.get(3, [])) == 0:
                    if (instance0_max_slack > 2) and (instance0_min_slack < 0.5):
                        slow_instance_id = 3
                        fast_instance_id = 2
                        need_rebalance = True
                    else:
                        need_rebalance = False
                else:
                    if (instance0_max_slack - instance0_min_slack <= 1.5) and (instance1_max_slack - instance1_min_slack <= 1.5):
                        need_rebalance = False
                    else:
                        need_rebalance = True
                        if (instance0_max_slack - instance0_min_slack <= 1.5) and (instance1_max_slack - instance1_min_slack > 1.5):
                            slow_instance_id = 2
                            fast_instance_id = 3
                        elif (instance1_max_slack - instance1_min_slack <= 1.5) and (instance1_max_slack - instance0_min_slack > 1.5):
                            slow_instance_id = 3
                            fast_instance_id = 2
                        else:
                            if (instance0_min_slack < instance1_min_slack):
                                slow_instance_id = 3
                                fast_instance_id = 2
                            else:
                                slow_instance_id = 2
                                fast_instance_id = 3

                print(f"***********  slow_instance_id: {slow_instance_id}, fast_instance_id: {fast_instance_id}")

                if need_rebalance and now - last_rebalance_time > 1:
                    try:
                        fast_instance_seqs = slack_map[fast_instance_id]
                        slow_instance_seqs = slack_map[slow_instance_id]

                        send_fast2slow_seqs, send_slow2fast_seqs = self.optimize_request_assignment(fast_instance_seqs=fast_instance_seqs,
                                                                                            slow_instance_seqs=slow_instance_seqs,
                                                                                            fast_instance_id=fast_instance_id,
                                                                                            slow_instance_id=slow_instance_id,
                                                                                            now=now)

                        if len(send_fast2slow_seqs) != 0 or len(send_slow2fast_seqs) != 0:
                            print(f"rebalance happens!!! len(send_fast2slow_seqs): {len(send_fast2slow_seqs)}, len(send_slow2fast_seqs): {len(send_slow2fast_seqs)}, send_fast2slow_seqs:{send_fast2slow_seqs}, send_slow2fast_seqs:{send_slow2fast_seqs}")

                            # Add elements from send_slow2fast_seqs to slow_instance_id's waiting_for_rebalance list
                            for seq_group, des_engine_id in send_slow2fast_seqs:
                                self.pd_engine[slow_instance_id].waiting_for_rebalance.append((seq_group, des_engine_id))

                            # Add elements from send_fast2slow_seqs to fast_instance_id's waiting_for_rebalance list
                            for seq_group, des_engine_id in send_fast2slow_seqs:
                                self.pd_engine[fast_instance_id].waiting_for_rebalance.append((seq_group, des_engine_id))

                        last_rebalance_time = time.time()

                    except Exception as e:
                        print(f"Error during rebalance: {e}")

            except Exception as e:
                print(f"Unexpected error: {e}")

            await asyncio.sleep(0.5)