这是我的问题:什么是最好和更聪明的方式知道“当一组特定任务完成后能够启动一些其他任务取决于组刚刚完成“Multithread如何知道什么时候所有任务完成(Python)
例如,我有这棵树,所以当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?
对于时刻,这是基于一定的基督徒,使用回调 http://www.chrisarndt.de/projects/threadpool/
对于每个字母我有任务数的lib线程池我在做什么,如此完成我已创建一个字典像
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)
print_result第一个请求会被调用时DO_事情结束
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()
,我是否instances_count [信] [0] == instances_count [信] [1]这意味着A的所有任务都完成,我可以在“开始”字母B和H亦然
所以有人可以告诉我,如果这是一个很好的解决方案?如果没有,我该如何改进?
谢谢!
[“其他线程可以调用线程的join()方法。这会阻塞调用线程,直到调用join()方法的线程终止。“](http://docs.python.org/2/library/threading.html#thread-objects) – martineau