简体   繁体   中英

Python Threading with multiple functions and list

My code is a much more complex version of the following. I will try to explain what I want as clearly as possible. consider I have the following code. few functions and list of numbers.

def main_fun(number):
    num = randint(1,10)
    sleep(num)
    return f"{num} --> {number}"


def fun_1(number):
    print(f"fun_1  :  {main_fun(number)}")

def fun_2(number):
    print(f"fun_2  :  {main_fun(number)}")

def fun_3(number):
    print(f"fun_3  :  {main_fun(number)}")

def fun_4(number):
    print(f"fun_4  :  {main_fun(number)}")

def fun_5(number):
    print(f"fun_5  :  {main_fun(number)}")


a = [1,2,3,4,5,6,7,8,9,10]

You can see that there is a list with 11 numbers and 6 functions. main_fun() is the main function. I want to call the main_fun() through the other 5 functions using threading.

I want the script to work like this, at first, the five functions will be called like this.(parallel using threading) i know how to do this. The next part is what tricky for me.

fun_1(1)
fun_2(2)
fun_3(3)
fun_4(4)
fun_5(5)

As you can see main_fun() will sleep for a radome amount of time before returning something. so, say main_fun() called through fun_2() returned something first. meaning fun_2() finished running, then I want to pass the next number in the list (which is 6) and call fun_2() again(fun_2(6)). And then, the function which finishes next need to be called with the next number in the list as an argument. I hope i explained it clearly. I have been stuck with this for the past 2 days. Thanks

I have found a solution. but not sure if it is the most efficient way. Here it is. I am sharing the entire code.

from random import randint
from time import sleep
import threading

def main_fun(number):
    num = randint(1,10)
    sleep(num)
    return f"{num} --> {number}"


def fun_1(number):
    print(f"fun_1  :  {main_fun(number)}")

def fun_2(number):
    print(f"fun_2  :  {main_fun(number)}")

def fun_3(number):
    print(f"fun_3  :  {main_fun(number)}")

def fun_4(number):
    print(f"fun_4  :  {main_fun(number)}")

def fun_5(number):
    print(f"fun_5  :  {main_fun(number)}")


a = [1,2,3,4,5,6,7,8,9,10]


a_iter = iter(a)

thread_list = []

list_end = False

for fun in [fun_1,fun_2,fun_3,fun_4,fun_5]:
    try:
        num = next(a_iter)
        thread = threading.Thread(target=fun,args=[num])
        thread_list.append([thread,fun])
        thread.start()
    except StopIteration:
        list_end = True
        break

while not list_end:
    try:
        for ind, [thread, fun] in enumerate(thread_list):
            thread: threading.Thread
            if not thread.is_alive():
                num = next(a_iter)
                thread_list[ind] = [threading.Thread(target=fun,args=[num]),fun]
                thread_list[ind][0].start()
    except StopIteration:
        list_end = True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM