job_queue.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:AlgorithmicToolbox 作者: chaicko 项目源码 文件源码
def assign_jobs(self):
        # Trivial case
        if len(self.jobs) <= self.num_workers:
            self._assigned_workers = [i for i in range(len(self._jobs))]
            self._start_times = [0] * len(self._jobs)
            return

        # Create Heap
        from collections import namedtuple
        import heapq
        MyThread = namedtuple('MyThread', 'start_time, id')
        heap = [MyThread(0, i) for i in range(self._num_workers)]
        heapq.heapify(heap)

        for i, job in enumerate(self._jobs):
            # Read the root contents
            sched_thread_id = heap[0].id
            sched_thread_start = heap[0].start_time
            # Append to output
            self._assigned_workers[i] = sched_thread_id
            self._start_times[i] = sched_thread_start
            # Update heap with next start time
            heapq.heapreplace(heap, MyThread(sched_thread_start + job, sched_thread_id))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号