Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
parallel loop python
#1
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)
Reply
#2
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.
Reply
#3
yes i want to split HourLoopRange and run in separate processes. what is your advice?
Reply
#4
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-...d-threads/
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas, How to trigger parallel loop Mekala 4 2,735 Oct-29-2020, 12:58 PM
Last Post: Mekala
  Matlab to Python -- Parallel Computing zistambo 1 1,970 Jun-10-2020, 04:59 PM
Last Post: pyzyx3qwerty
  Use dynamic variable from parallel running python script Sicksym 0 1,847 May-15-2020, 02:52 PM
Last Post: Sicksym
  Parallel iteration with for loop Josh_Python890 1 2,176 Jul-19-2019, 11:50 PM
Last Post: metulburr
  parallel for loop with multiprocessing dervast 0 2,019 Jul-04-2019, 03:16 PM
Last Post: dervast
  Parallel Processing in Python with Robot crcali 6 5,165 Apr-06-2018, 03:48 AM
Last Post: Larz60+
  parallel(offset) lines using python johnfriend 1 4,291 May-05-2017, 06:10 AM
Last Post: buran
  Parallel Python Manjunath 2 4,387 Oct-11-2016, 11:56 AM
Last Post: Manjunath

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020