import operator
import random
class Process:
    def __init__(self, name,  arrive_time, serve_time,):
        self.name = name
        self.arrive_time = arrive_time
        self.serve_time = serve_time
        self.left_serve_time = serve_time
        self.finish_time = 0
        self.cycling_time = 0
flag = 1

class Queue:
    def __init__(self, priority, processlist):
        self.priority = priority
        self.processlist = processlist
        self.q = 0

    def size(self):
        return len(self.processlist)

    def get(self, index):
        return self.processlist[index]

    def add(self, process):
        return self.processlist.append(process)

    def delete(self, index):

        return self.processlist.remove(self.processlist[index])

def Scan(queuelist, i, running_time):
    for j in range(i):
        index = 0
        curqueue = queuelist[j]
        if(curqueue == []):
            print((f"第{j}队列为空。"))
            break
        else:
            for k in range(curqueue.size()):
                if (curqueue.get(index).arrive_time > running_time):
                    print(f"当前以运行：{running_time},{curqueue.get(index).name}已到达")
                    if(i==2 and j ==1):
                        running_time = Scan(queuelist,j,running_time)
                    elif(i == 3 and j ==2 ):
                        running_time = Scan(queuelist, j, running_time)
                    curqueue.get(index).left_serve_time -=queuelist[j].q
                    running_time += queuelist[j].q
                    if (j == len(queuelist)-1):
                        queuelist[j].add(curqueue.get(index))
                        print(f"进程{curqueue.get(index).name}还未完成，已添加至{j}队列。")
                    else:
                        queuelist[j+1].add(curqueue.get(index))
                        print(f"进程{curqueue.get(index).name}还未完成，已添加至{j+1}队列。")
                elif(curqueue.get(index).left_serve_time ==queuelist[j].q):
                    running_time += queuelist[j].q
                    print(f"进程{curqueue.get(index).name}已完成弹出")
                    curqueue.get(index).left_serve_time = 0
                else:
                    running_time += curqueue.get(index).left_serve_time
                    print(f"进程{curqueue.get(index).name}已完成弹出")
                    curqueue.get(index).left_serve_time = 0
                if(curqueue.get(index).left_serve_time == 0):
                    curqueue.get(index).finish_time =running_time
                    curqueue.get(index).cycling_time = curqueue.get(index).finish_time-curqueue.get(index).arrive_time
                    print(f"{curqueue.get(index).name}进程已完成。完成时间：{curqueue.get(index).finish_time},周转时间：{curqueue.get(index).cycling_time}")
                    index -= 1
                    curqueue.delete(index)
                    index += 1
                    if index == curqueue.size():
                        break
    return running_time

def MFQ(queuelist, queuefirst, runningtime):
    global flag
    for i in range (0, len(queuelist)):
        if i == 0 :
            queuelist[0].q = queuefirst
        else:
            queuelist[i].q = (queuelist[i-1].q)*2
        index = 0
        curqueue = queuelist[i]
        if flag :
            runningtime = curqueue.get(0).arrive_time
            flag = 0
        if curqueue.size()==0:
            print("此队列为空。")
            if queuelist[0].size != 0:
                MFQ(queuelist, queuelist, runningtime)
        else:
            while True:
                if curqueue.get(index).arrive_time > runningtime:
                    break
                else:
                    if(i != 0):
                        runningtime = Scan(queuelist, i, runningtime)
                        runningtime = Scan(queuelist, i, runningtime)
                    if curqueue.get(index).left_serve_time > queuelist[i].q:
                        curqueue.get(index).left_serve_time -= queuelist[i].q
                        runningtime += queuelist[i].q

                        if i == len(queuelist)-1:
                            queuelist[i].add(curqueue.get(index))
                            print(f"进程{curqueue.get(index).name}未执行完毕，添加至{i}队列")
                        else:
                            queuelist[i+1].add(curqueue.get(index))
                            print(f"进程{curqueue.get(index).name}未执行完毕，添加至{i+1}队列")
                    elif curqueue.get(index).left_serve_time == queuelist[i].q:
                        runningtime += queuelist[i].q
                        print(f"进程{curqueue.get(index).name}已完成。")
                        curqueue.get(index).left_serve_time = 0
                    else :
                        runningtime += curqueue.get(index).left_serve_time
                        print(f"进程{curqueue.get(index).name}已完成。")
                        curqueue.get(index).left_serve_time = 0
                    if curqueue.get(index).left_serve_time == 0 :
                        curqueue.get(index).finish_time = runningtime
                        curqueue.get(index).cycling_time = curqueue.get(index).finish_time-curqueue.get(index).arrive_time
                        print(f"进程{curqueue.get(index).name}已完成。完成时间：{curqueue.get(index).finish_time}。周转使时间：{curqueue.get(index).cycling_time}。")
                        index -=1
                    curqueue.delete(index)
                    index +=1
                    if index == curqueue.size():
                        break


# 随机生成进程
def CreatePcb(queue_list):
    process_list0, process_list1, process_list2 = [], [], []

    num = random.randint(1, 5)
    print('进程数 %d' % num)

    name = random.sample('abcdefghijklmnopqrstuvwxyz', num)

    for i in range(num):
        arrive_time = random.randint(1, 10)
        serve_time = random.randint(1, 10)
        print('进程名%s 到达时间%d 服务时间%d' % (name[i], arrive_time, serve_time))
        process = Process(name[i], arrive_time, serve_time)
        process_list0.append(process)

    # 按照arrive time进行排序
    cmpfun = operator.attrgetter('arrive_time')
    process_list0.sort(key=cmpfun)

    queue_list = []
    queue0 = Queue(0, process_list0)
    queue1 = Queue(1, process_list1)
    queue2 = Queue(2, process_list2)
    queue_list.append(queue0)
    queue_list.append(queue1)
    queue_list.append(queue2)

    return queue_list


if __name__ == '__main__':
    # 队列
    queue_list = []

    # 创建就绪队列
    cp = CreatePcb(queue_list)

    # 执行多级反馈队列调度算法,第一队列时间片为1
    mfq = MFQ(cp, 1, 0)

请优化此程序