gpt4 book ai didi

Python - 具有多个数组的多线程将参数传递给函数

转载 作者:行者123 更新时间:2023-12-04 07:42:15 25 4
gpt4 key购买 nike

我正在尝试为一个非常耗时的程序实现多线程,我遇到了这个 SO 答案:
https://stackoverflow.com/a/28463266/3451339 ,它基本上为多个数组提供了这个解决方案:

from multiprocessing.dummy import Pool as ThreadPool

pool = ThreadPool(4)
results = pool.map(my_function, my_array)

# Close the pool and wait for the work to finish
pool.close()
pool.join()
并且,传递多个数组:
results = pool.starmap(function, zip(list_a, list_b))

以下是我到目前为止必须使用线程重构的代码。它迭代 4 个数组,并且需要在每次迭代时将参数传递给函数并将所有结果附加到最终容器中:
    strategies = ['strategy_1', 'strategy_2']
budgets = [90,100,110,120,130,140,150,160]
formations=['343','352','433','442','451','532','541']
models = ['model_1', 'model_2', 'model_3']

all_teams = pd.DataFrame()

for strategy in strategies:
for budget in budgets:
for formation in formations:
for model in models:

team = function(strategy=strategy,
budget=budget,
curr_formation=formation,
model=model)

all_teams = all_teams.append(team, ignore_index=True, sort=False)\
.reset_index(drop=True)\
.copy()

备注 : 每个函数调用都会发出 api web 请求。
在这种情况下使用多线程的方法是什么?

最佳答案

Python 有 multiprocessing module可以在 中运行多个任务平行在每个进程内你可以有多个线程或 async io代码
这是一个使用 3 个进程和多线程的工作示例

import pandas as pd
import multiprocessing
from multiprocessing import Queue
from threading import Thread

strategies = ['strategy_1', 'strategy_2']
budgets = [90,100,110,120,130,140,150,160]
formations=['343','352','433','442','451','532','541']
models = ['model_1', 'model_2', 'model_3']

#shared Queue if you want to reduce write locking use 3 Queues
Q = Queue()

# Retrive async if you want to speed up the process
def function(q,strategy,budget,curr_formation,model):
q.put("Team")

def runTask(model,q):
for strategy in strategies:
for budget in budgets:
for formation in formations:
Thread(target=function,args=(q,strategy,budget,formation,model)).start()

def main():
p1 = multiprocessing.Process(target=runTask, args=('model_1',Q))
p2 = multiprocessing.Process(target=runTask, args=('model_2',Q))
p3 = multiprocessing.Process(target=runTask, args=('model_3',Q))

p1.start()
p2.start()
p3.start()

p1.join()
p2.join()
p3.join()

all = []
for i in range(0,Q.qsize()):
all.append(Q.get())
print(all)
print(len(all))

if __name__ == "__main__":
main()
一篇有用的文章 Multiprocessing in Python | Set 2

关于Python - 具有多个数组的多线程将参数传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67393699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com