I want to use the return value of an object's method within the multiprocessing
module. From the documentation I thought that this can be realized by multiprocessing.pool.AsyncResult.get
.
But instead, an error is thrown in my case whereas normally an integer value should be returned.
Here's a minimal example:
import multiprocessing as mp
from random import randrange
class Model():
def __init__(self):
self.rno = randrange(100)
def solve(self, *args, **kwargs):
print(args, kwargs)
return self.rno
if __name__ == '__main__':
# sample data
models = [Model() for m in range(0, 10)]
args = [1, 2]
kwds = {'foo': 3, 'bar': 4}
# execution
pool = mp.Pool(processes=mp.cpu_count())
for m in models:
result = pool.apply_async(m.solve(*args, **kwds))
print(result.get())
pool.close()
pool.join()
Instead of delivering the attribute rno
of the object result.get()
throws an error TypeError: 'int' object is not callable
.
Any hints? What am I missing here?
Thanks in advance for your help!
PS: Note that in my real application the return value of the method solve()
delivers another data type whereas the structure changes dynamically. But it looks if I am generally missing something..
I found the solution on my own!
The error was that I have passed the result of the method to pool but not the function itself.
Here's a working example:
import multiprocessing as mp
from random import randrange
class Model():
def __init__(self):
self.rno = randrange(100)
def solve(self, *args, **kwargs):
print(args, kwargs)
return [self.rno, args, kwargs]
if __name__ == '__main__':
# sample data
models = [Model() for m in range(0, 10)]
args = [1, 2]
kwds = {'foo': 3, 'bar': 4}
pool = mp.Pool(processes=mp.cpu_count())
for m in models:
result = pool.apply_async(m.solve, args=args, kwds=kwds)
print(result.get())
pool.close()
pool.join()
Sorry for bothering you..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.