在threads python中多线程化并传递正确的示例变量

6tdlim6h  于 2023-03-06  发布在  Python
关注(0)|答案(1)|浏览(82)

我正在尝试基于存储在字典列表中的值启动多线程。在启动所有sthreads之后,我必须启动bthreads,基于回填值。我也需要在其他函数中传递定义的变量。由于for循环的作用域,我很难传递正确的示例,我将感谢任何关于如何保存定义的变量并能够调用它们的建议。我是否应该创建一个函数,并在循环中使用,然后如何访问它?抱歉,问了一些新手问题。
谢谢你。

vars_array = [{"name":"Jane", "age":5, "city":"Rome", "Backfill":"true"},{"name":"Josh", "age":15, "city":"Venice", "Backfill":"false"}]
for x in vars_array:
        name= x['name']
        age = x['age'],
        city = x['city'],
        Backfill=x['Backfill']

        sThread = threading.Thread(target = sendThread, args = (name, 
        age, city, Backfill))
        sThread.start()
        sthreads.append(sThread)
        for sThread in sthreads:
            sThread.join()    
            if Backfill == "true":
                bthreads=[] 
                bThread= threading.Thread(target=bSendThread, args = (name, 
                age, city, Backfill))
                bThread.start()
                bthreads.append(bThread)  
                for bThread in bthreads:
                    bThread.join()
eiee3dmh

eiee3dmh1#

您不是要使用vars_array中的变量吗?

此时,您使用第一个数组中Backfilllast 值来决定是否运行sThread,如果您 do 运行sThread,您将为它们提供与最终bThread相同的输入。
我想知道您是否打算为每个线程使用Backfill的 * 相应 * 值?

sthreads=[]
jConfig = load_config()
vars_array = jConfig['vars_array']
for x in vars_array:
        URL= x['URL']
        Host = x['Host'],
        Port = x['Port'],
        BackfillLimit = x['BackfillLimit'],
        Poll= x['Poll'],
        Backfill= x['Backfill'],
        Receiver=x['Receiver'],
        sThread = threading.Thread(target = sendThread, args = (Poll, Backfill, URL, Host, BackfillLimit,Receiver))
        sThread.start()
        sthreads.append(sThread)

for sThread,x in zip(sThreads,vars_array):
        sThread.join()    
        URL= x['URL']
        Host = x['Host'],
        Port = x['Port'],
        BackfillLimit = x['BackfillLimit'],
        Poll= x['Poll'],
        Backfill= x['Backfill'],
        Receiver=x['Receiver'],
        if Backfill == "true":
            bthreads=[] 
            bThread= threading.Thread(target=bSendThread, args = (Poll, Backfill, URL, Host, BackfillLimit, Receiver))
            bThread.start()
            bthreads.append(bThread)  
            for mbThread in bthreads:
                bThread.join()

另外,为什么不延迟bThread的加入以优化性能呢?

首先启动所有bThread,然后在所有bThread都启动后才加入它们。

sthreads=[]
jConfig = load_config()
vars_array = jConfig['vars_array']
for x in vars_array:
        URL= x['URL']
        Host = x['Host'],
        Port = x['Port'],
        BackfillLimit = x['BackfillLimit'],
        Poll= x['Poll'],
        Backfill= x['Backfill'],
        Receiver=x['Receiver'],
        sThread = threading.Thread(target = sendThread, args = (Poll, Backfill, URL, Host, BackfillLimit,Receiver))
        sThread.start()
        sthreads.append(sThread)

bThreads=[] 
for sThread,x in zip(sThreads,vars_array):
        sThread.join()    
        URL= x['URL']
        Host = x['Host'],
        Port = x['Port'],
        BackfillLimit = x['BackfillLimit'],
        Poll= x['Poll'],
        Backfill= x['Backfill'],
        Receiver=x['Receiver'],
        if Backfill == "true":
            bThread= threading.Thread(target=bSendThread, args = (Poll, Backfill, URL, Host, BackfillLimit, Receiver))
            bThread.start()
            bThreads.append(bThread)  
              

for mbThread in bThreads:
     bThread.join()

相关问题