AJN
AJN

Reputation: 1206

Python threading: Running task concurrently inside sequential sections

I have multiple sections in my code that I need to perform sequentially, section 2 runs only after section 1 tasks finished. But inside section 1 I want to use threading to run tasks concurently.

Here is the program:

#!/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.'

The issue :

What I need is to run section 1 tasks concurrently and only when all finished , it passes to section 2

I am missing something here, any idea?

Upvotes: 0

Views: 2388

Answers (1)

dano
dano

Reputation: 94951

Just call join on each task after you've started them all:

# 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()

Upvotes: 1

Related Questions