Issue
I am trying and failing to run the following computation in python. I have a class, let's call it class Telescope, which has some method, compute_something(self, args). Inside the same class I have a method, simulation, which needs to call compute_something eight times with different arguments. Each of these runs is independent from each other, so I could just run them at the same time, in order to speed up time. Does anyone know how to do this?
Using the same code that was proposed in one of the answers. Basically I have some code like:
import multiprocessing as mp
class Telescope:
def __init__(self, num):
self.num = num
def compute_something(self, a, b, c):
print('Start')
res = self.num * a * b * c
print('Finish')
return res
def simulation(self):
starmap_in = []
#Populate starmap_in
for i in range(5):
starmap_in.append(list(range(i, i+3)))
with mp.Pool(5) as pool:
print(pool.starmap(self.compute_something, starmap_in))
x = Telescope(10)
x.simulation()
I have tried what was proposed in the first answer (using locks) and also trying to use multiprocessing it as if there was not any classes involved but this has not been successful, the program starts running but it hangs as if there was there forever.
This same thing happens with the proposed code, it would work for the first 5-6 times I run it, but after this I need to restart the kernel.
Running on Linux.
Solution
What you need is the "Pool" class from the multiprocessing library
Here's some example code:
import multiprocessing as mp
class Telescope:
def compute_something(self, a, b, c):
return a * b * c #Do your computations here
def simulation(self):
#The input to starmap needs to be formatted like this: [[arg1 for first call, arg2 for first call, arg3 for first call], [arg1 for second call, arg2 for second call, arg3 for second call]]
starmap_in = []
#Populate starmap_in
for i in range(5):
starmap_in.append(list(range(i, i+3)))
print(starmap_in)
with mp.Pool(5) as pool:
print(pool.starmap(self.compute_something, starmap_in)) #This is what calls the function 5 times in paralell
x = Telescope()
x.simulation()
However if you will be using the self variable in your function, doing it like this is a really bad idea. You need locks to do this if you want to access self.
import multiprocessing as mp
def init_pool_processes(the_lock):
#Initialize each process with a global variable lock.
global lock
lock = the_lock
class Telescope:
def __init__(self, num):
self.num = num
def compute_something(self, a, b, c):
lock.acquire()
res = self.num * a * b * c #Do your computations here
lock.release()
return res
def simulation(self):
lock = mp.Lock()
#The input to starmap needs to be formatted like this: [[arg1 for first call, arg2 for first call, arg3 for first call], [arg1 for second call, arg2 for second call, arg3 for second call]]
starmap_in = []
#Populate starmap_in
for i in range(5):
starmap_in.append(list(range(i, i+3)))
with mp.Pool(5, initializer=init_pool_processes, initargs=(lock,)) as pool:
print(pool.starmap(self.compute_something, starmap_in)) #This is what calls the function 5 times in paralell
x = Telescope(10)
x.simulation()
Edit: Quoted fron the python docs:
Make sure that the main module can be safely imported by a new Python interpreter without causing unintended side effects (such a starting a new process). ... one should protect the “entry point” of the program by using if name == 'main':
This would make your code look like this:
import multiprocessing as mp
class Telescope:
def compute_something(self, a, b, c):
return a * b * c #Do your computations here
def simulation(self):
#The input to starmap needs to be formatted like this: [[arg1 for first call, arg2 for first call, arg3 for first call], [arg1 for second call, arg2 for second call, arg3 for second call]]
starmap_in = []
#Populate starmap_in
for i in range(5):
starmap_in.append(list(range(i, i+3)))
print(starmap_in)
with mp.Pool(5) as pool:
print(pool.starmap(self.compute_something, starmap_in)) #This is what calls the function 5 times in paralell
if __name__ == '__main__':
x = Telescope()
x.simulation()
Answered By - drake14w Answer Checked By - Clifford M. (Python.Easybugfix Volunteer)
Post a Comment
Post a Comment