Simple multi-threaded loop in Python

I searched everywhere and did not find any simple example of loop iteration with multithreading.

For example, how can I multithread this loop?

for item in range(0, 1000): print item 

Is there a way to cut it as 4 threads so that each thread has 250 iterations?

+16
python multithreading iteration
source share
3 answers

The easiest way is to use multiprocessing.dummy (which uses threads instead of processes) and a pool .

 import multiprocessing.dummy as mp def do_print(s): print s if __name__=="__main__": p=mp.Pool(4) p.map(do_print,range(0,10)) # range(0,1000) if you want to replicate your example p.close() p.join() 

You might want to try true multiprocessing if you want to use multiple processors better, but there are a few caveats and recommendations to follow.

Other Pool methods may be better suited to your needs - depending on what you are actually trying to do.

+11
source share

You will need to do the splitting manually:

 import threading def ThFun(start, stop): for item in range(start, stop): print item for n in range(0, 1000, 100): stop = n + 100 if n + 100 <= 1000 else 1000 threading.Thread(target = ThFun, args = (n, stop)).start() 

This code uses multithreading, which means that everything will be executed within one Python process (i.e. only one Python interpreter will be launched).

Multiprocessing, discussed in another answer, means running some code in multiple Python interpreters (in multiple processes, not in threads). It can use all available CPU cores, so it’s useful when you focus on the speed of your code (print a ton of numbers until the terminal hates you!), And not just in parallel processing. one


1. multiprocessing.dummy turns out to be a wrapper around the threading module . multiprocessing and multiprocessing.dummy have the same interface, but the first module performs parallel processing using processes, and the second using threads.

+8
source share

This will split your target list into chunks and trigger individual threads. More info here :

 import concurrent.futures mybiglist=['abc','def','ghi','jkl','mno'] mytargetlist=['abc','jkl'] def get_index(x): return mybiglist.index(x) with concurrent.futures.ProcessPoolExecutor() as executor: results = executor.map(get_index, mytargetlist) print(list(results)) 
0
source share

All Articles