Home > Net >  Multithreading for double list
Multithreading for double list

Time:05-19

I m new to programming, I have searched and looked at other SO posts regarding multithread, my title may be misleading but I'll try to explain in brief!

I want to understand how I keep passing 2 args from 2 different lists.

so for example.

If I have 2 lists.

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

And I want to loop through this list and pass those in a function but using multithreading.

normal for loop would be something like this..

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

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

But I want to use multithreading for this process, so it would print all both list value together...

so far I've tried this but its not working.

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()

Can someone please explain to me how multi-threading works in a simple way? And How do I achieve the above results?

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?

CodePudding user response:

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 does not benefit of threading in CPU-Bound operations. For that you would need Multiprocessing.

Beware that in windows it gets very complex very fast, and it is very slow to launch processes. To the point that unless it is taking multiple seconds to process it does not help. Besides ProcessPoolExecutor you also have Starmap from multiprocessing library.

  • Related