0

I have a list of objects and I want to call a method of these objects iteratively in a for loop but to speed up things I need to run this on parallel. Here is an example:

class myObj:
    def __init__(self):
        self.x = 0

    def go(self):
        self.x += 1

myList = [myObj() for i in range(100)]
for t in range(1000):
    for obj_ in myList:
        obj_.go()

How can I run the second loop in parallel? because these objects are distinct it should be possible.

Alireza
  • 410
  • 3
  • 17
  • 1
    Can you share a bit more about your actual program? Performance and parallelism are complicated and finicky topics. – AMC Mar 14 '20 at 23:48
  • @AMC My actual code has almost the same structure, I have ~1000 objects which I want to recursively call a method of them and then do some other calculations. You can think of them as 1000 distinct random walkers which take a step when their "go" method is called. – Alireza Mar 14 '20 at 23:51
  • It’s up to you. In that case, I would recommend looking at some guides or tutorials, Stack Overflow is not really the place for those kinds of things (see [ask], [help/on-topic]). – AMC Mar 15 '20 at 01:24
  • @AMC Why? My question is very clear even without giving more details and it hasn't been asked previously on SO, it is not homework and I have already explored possible options prior to posting a question. I believe my question is legit. – Alireza Mar 15 '20 at 08:21

1 Answers1

0

One way to do this using python 3.x would be to use the multiprocessing.Pool and map method to apply the myObj.go method to every element in your myList. The processes parameter for constructing Pool can be changed according to the number of parallel processes you would like to use, normally based on the available number of cores on your computer. Note that in order to keep the result of updating myObj.x on each iteration, I have revised the go method to return self:

import multiprocessing as mp                                                    

class myObj:                                                                    
    def __init__(self):                                                         
        self.x = 0                                                              

    def go(self):                                                               
        self.x += 1                                                             
        return self                                                             

if __name__ == "__main__":                                                      
    myList = [myObj() for i in range(100)]                                      

    with mp.Pool(processes=4) as pool:                                          
        for t in range(1000):                                                   
            myList = pool.map(myObj.go, myList)                                 

You could also have a read of this answer for some more possible approaches.

dspencer
  • 4,297
  • 4
  • 22
  • 43