简体   繁体   English

Python 具有多个函数和列表的线程

[英]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.你可以看到有一个包含 11 个数字和 6 个函数的列表。 main_fun() is the main function. I want to call the main_fun() through the other 5 functions using threading. main_fun() 是主要的 function。我想使用线程通过其他 5 个函数调用 main_fun()。

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.如您所见,main_fun() 会在返回某些内容之前休眠一段时间。 so, say main_fun() called through fun_2() returned something first.所以,假设通过 fun_2() 调用的 main_fun() 首先返回了一些东西。 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)).意味着 fun_2() 完成运行,然后我想传递列表中的下一个数字(即 6)并再次调用 fun_2()(fun_2(6))。 And then, the function which finishes next need to be called with the next number in the list as an argument.然后,需要以列表中的下一个数字作为参数调用下一个完成的 function。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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