我的代码中有多个部分需要按顺序执行,第2部分仅在第1部分任务完成后运行。但在第1节中,我想使用线程来执行任务。
以下是该计划:
#!/usr/bin/env python
import threading
import random
import time
import logging
logging.basicConfig(level=logging.DEBUG)
def myfunction(num, t):
logging.debug('myfunction number ' + str(num) + ' Started')
time.sleep(t)
logging.debug('myfunction number ' + str(num) + ' finished: Took ' + str(t) + ' seconds')
return
print 'Section 1:'
# List of tasks to do
task_list = []
start_time = time.time()
# Create the number of tasks we need to perform
for i in range(5):
# Define the task as executing the function myfunction with a random execution time
task_time = random.randint(1, 9)
task = threading.Thread(target=myfunction, args=(i, task_time))
#task.daemon = True
# append each task to the task list
task_list.append(task)
# start the task
task.start()
# (1)
# calling join for each task will make the
# the program wait for each task to finish before the next ==> sequential execution
# task.join()
print 'Section 2: requires section 1 to finish before continuting.'
问题:
我需要的是同时运行第1部分任务,并且只有在完成所有任务时,它才会传递到第2部分
我在这里遗漏了什么,有什么想法吗?
答案 0 :(得分:1)
在启动后,只需在每个任务上调用join
:
# Create the number of tasks we need to perform
for i in range(5):
# Define the task as executing the function myfunction with a random execution time
task_time = random.randint(1, 9)
task = threading.Thread(target=myfunction, args=(i, task_time))
#task.daemon = True
# append each task to the task list
task_list.append(task)
# start the task
task.start()
# All tasks are now started, we can wait for each to finish without
# causing sequential execution
for task in task_list:
task.join()