Optimized algorithm to schedule tasks with dependency?

后端 未结 5 2103
梦毁少年i
梦毁少年i 2021-01-30 17:50

There are tasks that read from a file, do some processing and write to a file. These tasks are to be scheduled based on the dependency. Also tasks can be run in parallel, so the

5条回答
  •  不思量自难忘°
    2021-01-30 18:35

    Without considering the serial/parallel aspect of the problem, this code can at least determine the overall serial solution:

    def order_tasks(num_tasks, task_pair_list):
        task_deps= []
        #initialize the list
        for i in range(0, num_tasks):
            task_deps[i] = {}
    
        #store the dependencies
        for pair in task_pair_list:
            task = pair.task
            dep = pair.dependency
    
            task_deps[task].update({dep:1})
    
        #loop through list to determine order
        while(len(task_pair_list) > 0):
            delete_task = None
    
            #find a task with no dependencies
            for task in task_deps:
                if len(task_deps[task]) == 0:
                    delete_task = task
                    print task
                    task_deps.pop(task)
                    break
    
            if delete_task == None:
                return -1
    
            #check each task's hash of dependencies for delete_task
            for task in task_deps:
                if delete_key in task_deps[task]:
                    del task_deps[task][delete_key]
    
        return 0
    

    If you update the loop that checks for dependencies that have been fully satisfied to loop through the entire list and execute/remove tasks that no longer have any dependencies all at the same time, that should also allow you to take advantage of completing the tasks in parallel.

提交回复
热议问题