Python 在项目列表上使用线程

Python 在项目列表上使用线程,python,multiprocessing,Python,Multiprocessing,我有一个我想用线程处理的项目列表,每个项目大约需要10分钟,我想一次处理两个 现在我一次做一个,这需要一段时间 old = ["old_item1","old_item2","old_item3","old_item4","old_item5"] new = ["new_item1","new_item2","new_item3","new_item4","new_item5"] area = ["area_item1","area_item2","area_item3","area_item4

我有一个我想用线程处理的项目列表,每个项目大约需要10分钟,我想一次处理两个

现在我一次做一个,这需要一段时间

old = ["old_item1","old_item2","old_item3","old_item4","old_item5"]
new = ["new_item1","new_item2","new_item3","new_item4","new_item5"]
area = ["area_item1","area_item2","area_item3","area_item4","area_item5"]

def func(x,y,z):
    print (x,y,z)

d = zip(old,new, area)

for e,f, g in d: 
  thread1 = threading.Thread(target=func, args=(e,f,g))
  thread2 = threading.Thread(target=func, args=(e,f,g))
  thread1.start()
  thread2.start()
  thread1.join()
  thread2.join()
所以我想做的是过程

带有thread1的item1 带螺纹2的项目2

项目3与线程1 项目4与螺纹2

最后 项目5与thread1一起使用,您可以使用,它将为您处理所有繁琐的工作。使用该函数,可以对iterable的所有项运行函数作为其参数

比如:

来自多处理导入池的

old=[“old_item1”、“old_item2”、“old_item3”、“old_item4”、“old_item5”]
新建=[“新建项目1”、“新建项目2”、“新建项目3”、“新建项目4”、“新建项目5”]
面积=[“面积项目1”、“面积项目2”、“面积项目3”、“面积项目4”、“面积项目5”]
定义函数(x,y,z):
打印(x、y、z)
d=拉链(旧、新、面积)
使用Pool()作为池:
res=pool.starmap(func,d)
  • 这里实际上不需要保存结果,但通常返回值是与给定iterable对应的所有结果的列表

这种关联是否有特殊原因,或者您只是想多处理任务?正如我所说,每个项目大约需要10分钟来处理。它的cpu和内存都很低,只需要一段时间。因此,我想将时间一分为二,而不是50分钟。我知道我可以制作多个列表,可能还有多个重复的函数,我在想可能有一种更优雅的方式,可以让你在两个线程中执行两次相同的任务??在同一个迭代中,使用相同的值启动两个线程