Python Forum
parallel loop python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: parallel loop python (/thread-37456.html)



parallel loop python - caro - Jun-12-2022

Hello,
I have a funtion with a for loop, and i would like that the loop will be process faster and to make each loop run by a process so that it will be parallel.
what is the best way to do it in python.
would be glad to have examples.
thank you.
(i have tried lambda but it seems that my code doesn't match to use it)

def myloop():

   for HourLoop in HourLoopRange:                                                    

        app.BarP(HourLoop,len(HourLoopRange))

        root.update_idletasks()

        if bug==1: start2 = time.time()                                               

        if bug==1: app.enter_text("%f\n" % (time.time() - start2))                    

        LOAD_CH   (HourLoop,loads,iarray,load,carray,iarray99Cap,load99)               

        if bug==1: app.enter_text("%f\n" % (time.time() - start2))                    

        MACH_CH   (HourLoop,machs,Miarray,rarray,McarrayID,Mcarray,MiarrayOW[0])      

        if bug==1: app.enter_text("%f\n" % (time.time() - start2))                    

        STORAGE_CH(HourLoop,machs,Miarray,rarray,McarrayID,Mcarray,MiarrayOW[0])                                                                                                 

    SOF(HourLoop)



RE: parallel loop python - woooee - Jun-13-2022

Do you want to split HourLoopRange and run in separate processes?
Also, you have 4 "if bug==1" statements in a row. If you are entering text from the keyboard, that will be your hangup.


RE: parallel loop python - caro - Jun-15-2022

yes i want to split HourLoopRange and run in separate processes. what is your advice?


RE: parallel loop python - rob101 - Jun-15-2022

Not that I intend to step on anyone's toes here, but it sounds to me like you should be looking at Multithreading.

FYI:
A thread is the smallest unit of execution you can have with an independent instruction set; each thread performing a particular task with a starting point, an execution sequence and a result.

Threads within the same process share memory as well as the process state and run synchronously in a single process.

In general, the process will have one thread (the main thread that is always running) which creates the sub-thread objects.

Python's multithreading is pretty user-friendly and the fundamentals can be learned fairly quickly.

A good overview can be found here: https://www.pythontutorial.net/advanced-python/differences-between-processes-and-threads/


RE: parallel loop python - woooee - Jun-16-2022

Try multiprocessing with args=each sublist https://pymotw.com/3/multiprocessing/basics.html multiprocessing.cpu_count() will tell you how many cores your computer has=how many separate processes to start. Also take a look at multiprocessing.Pool in the docs from the link above.