Python线程:在顺序部分内同时运行任务

时间:2014-11-18 18:28:51

标签: python multithreading python-2.7

我的代码中有多个部分需要按顺序执行,第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部分中为每个任务(内部for循环)调用join,则任务将按顺序运行,并且不会从线程获得任何收益。
  • 如果我不在任何地方使用joing,程序会在完成第1部分任务的同时转到第2部分。

我需要的是同时运行第1部分任务,并且只有在完成所有任务时,它才会传递到第2部分

我在这里遗漏了什么,有什么想法吗?

1 个答案:

答案 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()