双列表的多线程

Multithreading for double list

我是编程新手,我搜索并查看了有关多线程的其他 SO 帖子,我的标题可能会产生误导,但我会尽量简要解释!

我想了解如何不断传递来自 2 个不同列表的 2 个参数。

举个例子。

如果我有 2 个列表。

l1 = ['a','b','c']
l2 = ['d','e','f']

我想遍历此列表并在函数中传递它们,但使用多线程。

正常的for循环是这样的..

def do_something(l1, l2):
    print(l1)
    print(l2)

for l1, l2 in zip(l11, l22):
  do_something(l11, l22)

但我想在此过程中使用多线程,因此它会同时打印所有两个列表值...

到目前为止,我已经试过了,但没有用。

threads = [] 
for l1, l2 in zip(l11, l22):    
    threads.append(threading.Thread(target=do_something, args=(l11, l22)))  
    threads[-1].start()
    
for t in threads:                                                           
    t.join()

有人可以简单地向我解释一下 multi-threading 是如何工作的吗? 以及如何实现上述结果?

从更大的角度思考,如果我比较多个 2 个巨大的 excel 文件,我必须在一个函数中为两个 excel 提供列名。我怎样才能 运行 并行运行函数,这样它就不会等待第一次迭代完成?

Thinking from a larger point of view if I m comparing multiple 2 huge excel files where I have to give column names for both excel in a function. How can I run the function parallelly so it wont wait for the first iteration to finish?

Python 不会受益于 CPU-Bound 操作中的线程化。为此,您需要多处理。

请注意,在 windows 中,它变得非常复杂非常快,并且启动进程非常 很慢。除非它需要几秒钟来处理它,否则它无济于事。 除了ProcessPoolExecutor you also have Starmap from multiprocessing library.