def multiprocess_init(global_message_queue: multiprocessing.Queue, global_control_queue: multiprocessing.Queue, total_job_number):
    logged_times = []
    log_repeat = 0
    alpha = 0.9
    second_schedule_finished = False
    map_out_to_in = {}
    map_in_to_out = {}
    global job_num
    job_num = 0
    while True:
        if (not global_message_queue.empty()):
            global_message = global_message_queue.get()
            job_id = global_message[0]
            message_type = global_message[1][0]
            message_graph = global_message[1][1]
            if (message_type == 0):
                job_num += 1
                map_out_to_in[job_id] = (job_num - 1)
                map_in_to_out[(job_num - 1)] = job_id
                job_id_in = (job_num - 1)
                logged_times.append([])
                global_graphs.append(message_graph)
                tensor_num = len(message_graph)
                for i in range(tensor_num):
                    logged_times[job_id_in].append([message_graph[i][6]])
                s = time.time()
                if (job_num == total_job_number):
                    (release_order, swap_order, recomputation_order) = generate_scheduling_plan(logged_times, 0)
                    print(f'time:{(time.time() - s)}')
                    control_messages = {}
                    for i in range(job_num):
                        control_message = [swap_order[i], release_order[i], recomputation_order[i]]
                        control_messages[map_in_to_out[i]] = control_message
                    global_control_queue.put(control_messages)
            else:
                job_id_in = map_out_to_in[job_id]
                total_time_old = 0
                for run_time in logged_times[job_id_in]:
                    total_time_old += run_time[0]
                total_time_new = 0
                for run_time in message_graph:
                    total_time_new += run_time[1]
                change_rate = (abs((total_time_new - total_time_old)) / total_time_old)
                print('change rate is ', change_rate)
                if (change_rate > 0.3):
                    is_replan = True
                else:
                    is_replan = False
                log_repeat += 1
                if ((log_repeat > 0) and (is_replan or (not second_schedule_finished))):
                    second_schedule_finished = True
                    for node_message in message_graph:
                        time_new = ((node_message[1] * alpha) + (logged_times[job_id_in][node_message[0]][0] * (1 - alpha)))
                        logged_times[job_id_in][node_message[0]] = [time_new]
                    (release_order, swap_order, recomputation_order) = generate_scheduling_plan(logged_times, 0)
                    print(logged_times)
                    control_messages = {}
                    for i in range(job_num):
                        print(swap_order)
                        control_message = [swap_order[i], release_order[i], recomputation_order[i]]
                        control_messages[map_in_to_out[i]] = control_message
                    global_control_queue.put(control_messages)
def multiprocess_init(global_message_queue: multiprocessing.Queue, global_control_queue: multiprocessing.Queue, total_job_number):
    logged_times = []
    log_repeat = 0
    alpha = 0.9
    second_schedule_finished = False
    map_out_to_in = {}
    map_in_to_out = {}
    global job_num
    job_num = 0
    while True:
        if (not global_message_queue.empty()):
            global_message = global_message_queue.get()
            job_id = global_message[0]
            message_type = global_message[1][0]
            message_graph = global_message[1][1]
            if (message_type == 0):
                job_num += 1
                map_out_to_in[job_id] = (job_num - 1)
                map_in_to_out[(job_num - 1)] = job_id
                job_id_in = (job_num - 1)
                logged_times.append([])
                global_graphs.append(message_graph)
                tensor_num = len(message_graph)
                for i in range(tensor_num):
                    logged_times[job_id_in].append([message_graph[i][6]])
                s = time.time()
                if (job_num == total_job_number):
                    (release_order, swap_order, recomputation_order) = generate_scheduling_plan(logged_times, 0)
                    print(f'time:{(time.time() - s)}')
                    control_messages = {}
                    for i in range(job_num):
                        control_message = [swap_order[i], release_order[i], recomputation_order[i]]
                        control_messages[map_in_to_out[i]] = control_message
                    global_control_queue.put(control_messages)
            else:
                job_id_in = map_out_to_in[job_id]
                total_time_old = 0
                for run_time in logged_times[job_id_in]:
                    total_time_old += run_time[0]
                total_time_new = 0
                for run_time in message_graph:
                    total_time_new += run_time[1]
                change_rate = (abs((total_time_new - total_time_old)) / total_time_old)
                print('change rate is ', change_rate)
                if (change_rate > 0.3):
                    is_replan = True
                else:
                    is_replan = False
                log_repeat += 1
                if ((log_repeat > 0) and (is_replan or (not second_schedule_finished))):
                    second_schedule_finished = True
                    for node_message in message_graph:
                        time_new = ((node_message[1] * alpha) + (logged_times[job_id_in][node_message[0]][0] * (1 - alpha)))
                        logged_times[job_id_in][node_message[0]] = [time_new]
                    (release_order, swap_order, recomputation_order) = generate_scheduling_plan(logged_times, 0)
                    print(logged_times)
                    control_messages = {}
                    for i in range(job_num):
                        print(swap_order)
                        control_message = [swap_order[i], release_order[i], recomputation_order[i]]
                        control_messages[map_in_to_out[i]] = control_message
                    global_control_queue.put(control_messages)