控制下一步的数据结构

2024-03-29 15:02:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习协同程序

class Scheduler:
    def __init__(self):
        self.ready = Queue() # a queue of tasks that are ready to run.
        self.taskmap = {} #dictionary that keeps track of all active tasks (each task has a unique integer task ID)

    def new(self, target): #introduce a new task to the scheduler
        newtask = Task(target)
        self.taskmap[newtask.tid] = newtask
        self.schedule(newtask)
        return newtask.tid

    def schedule(self, task):
        self.ready.put(task)

    def mainloop(self):
        while self.taskmap: #does not remove element from taskmap
            task = self.ready.get() self.ready
            result = task.run()
            self.schedule(task)

当我读到schedule中的task = self.ready.get()时,我突然意识到数据结构的本质是控制,控制下一步,而算法的本质也是控制,控制所有的步骤。你知道吗

这种理解有意义吗?你知道吗


Tags: oftorunselftargetnewtaskthat
1条回答
网友
1楼 · 发布于 2024-03-29 15:02:30

Queue对象定义了下一步的控制,是的。它是FIFO,正如所描述的here.

在这里,看起来您只是试图跟踪任务,是否还有剩余的任务,哪些任务正在执行,等等。这是“控制所有的步骤”。是的。你知道吗

不清楚的是目的。数据结构和算法应该适合您的目的。asyncio可以帮助您实现并行性和事件驱动设计。有时,目标是快速有效地将数据从源呈现到数据结构中。在最终目标的背景下,你得到的东西更有意义(至少对我来说)。你知道吗

相关问题 更多 >