0

I have 2 different methods. Each of the 2 methods return a list. One of the methods also takes in a parameter.

I am executing the two methods in parallel.

  1. However I do not understand how to get the return values of the two functions from my function call ?

  2. Also I got those code from elsewhere, and I do not understand what exactly is the significance of "is_something1" and "is_something2" and what is its role here?

from concurrent.futures import ThreadPoolExecutor
import datetime
import time


def func1():
    print("function1 called "+ str(datetime.datetime.now())+"\n")
    time.sleep(5)
    print("function1 ended "+ str(datetime.datetime.now())+"\n")
    list1 = [4,5,6]
    return list1

def func2(str_sample):
    print("function2 called "+ str(datetime.datetime.now())+"\n")
    time.sleep(5)
    print("function2 ended "+ str(datetime.datetime.now())+"\n")
    list2 = [1,2,3]
    return list2

def run_io_tasks_in_parallel(tasks):
    with ThreadPoolExecutor() as executor:
        running_tasks = [executor.submit(task) for task in tasks]
        for running_task in running_tasks:
            running_task.result()

results = run_io_tasks_in_parallel([
    lambda: {'is_something1': func1()},
    lambda: {'is_something2': func2()},
]) 

  • Either with [multiprocessing](https://docs.python.org/3/library/multiprocessing.html) or with [multithreading](https://docs.python.org/3/library/threading.html). Note that multithreading may not increase execution speed, if the task is CPU-bound. – t.m.adam Jul 25 '19 at 14:40
  • Thanks but I have actually edited my questions. Would you be able to assist on the above? – Vaishnavi Ganesh Jul 25 '19 at 15:02
  • 2
    The top results on google for both `multiprocessing` and `multithreading` have many solutions - https://stackoverflow.com/questions/10415028/how-can-i-recover-the-return-value-of-a-function-passed-to-multiprocessing-proce, https://stackoverflow.com/questions/6893968/how-to-get-the-return-value-from-a-thread-in-python – Peter Jul 25 '19 at 15:05
  • Hey @Peter. Thanks, but I am trying to call two different functions here. The links you posted only call one function many times – Vaishnavi Ganesh Jul 25 '19 at 15:19
  • With the line `multiprocessing.Process(target=worker)`, `worker` is the function. The whole point is you can run the code with whatever functions you want, they're just using loops to avoid the examples becoming too complex. – Peter Jul 25 '19 at 15:25

0 Answers0