这是我的问题:了解“当一组特定任务完成时能够根据刚刚完成的组开始其他任务”的最佳和更聪明的方法是什么
例如我有这棵树,所以当 A 完成时,我可以启动 H 和 B 等......但是在我下面的小树上,你还需要知道每个被认为完成的字母都必须执行某些任务(也是并行的) )
A
/ \
H B
/ / \
C F D
所以一个完整的例子是:
- Starting A
- Starting instance 1 of A
- Starting instance 2 of A
- Starting instance 3 of A
- Instance 1 of A Finished
- Instance 2 of A Finished
- Instance 3 of A Finished
- Starting H
- Starting B
- Starting instance 1 of H
- Starting instance 2 of H
- Starting instance 1 of B
- Starting instance 3 of H
- B has completed all its tasks
- Starting F
- Starting instance 4 of H
- H has completed all its tasks
- Starting C
etc...
所以继续我的问题:等待 A 的所有任务完成以启动 B 和 H 的最佳结构是哪个?
目前这是我基于某个基督徒的 lib 线程池所做的,使用回调 http://www.chrisarndt.de/projects/threadpool/
对于每个字母,我都有要完成的任务数,所以我创建了一个这样的字典
instances_count = {"A":[0,5],"B":[0,2],"H":[0,3],"C":[0,0],"D":[0,1]}
像这样提出我的第一个请求
requests = threadpool.makeRequests(do_something, ["A"],print_result)
当 do_something 完成时将调用 print_result
def do_something(data):
Here I just retrieve what the Tasks of A and make an other request to parallelize them
command_requests = threadpool.makeRequests(execute_command, argument_list,instance_finished)
def execute_command(data):
Here I Do what the task has to do and when it is done i return the datas
return data
def instance_finished():
And here what I do is that I will use that instance_count to add +1 when A task reletated to a Letter finishes...
Global.Lock()
instances_count[Letter][0] = instances_count[Letter][0] + 1
Global.unLock()
我检查 instance_count[Letter][0] == instances_count[Letter][1] 是否意味着 A 的所有任务都已完成,我可以“开始”字母 B 和 H
那么有人可以告诉我这是否是一个好的解决方案吗?如果没有,我该如何改进?
谢谢 !