Multiprocessing Error With Results
I have a small problem in a big program, so I made a small example, which shows my problem: import multiprocessing class class1(): def classfunction1(self, a): self.x
Solution 1:
The problem is you're trying to access the x
attribute of the class1
instance of class1
(!) that has never been created in the worker subprocesses because class1.classfunction1()
isn't called in them.
To fix that issue I added an init()
function and call it both in the main process and specify it as the initializer
function for each of the subprocesses created by the Pool
:
import multiprocessing
classclass1():
defclassfunction1(self, a):
self.x = a
classclass2():
defclassfunction2(self, a):
self.y = a
deftest(i):
print("I'm in the Testfunction")
b = i * class1.x * class2.y
return b
definit(): # addedglobal class1, class2
class1 = class1()
class2 = class2()
x = 1
y = 2
class1.classfunction1(x)
class2.classfunction2(y)
if __name__ == "__main__":
init() # explicit call hereprint("This variable is callable", class1.x)
print("And this one is also callable", class2.y)
counter = []
for i inrange(10):
counter.append(i)
pool = multiprocessing.Pool(initializer=init, processes=4) # implicit call
results = pool.imap(test, counter)
pool.close()
pool.join()
resultslist = list(results)
print(resultslist)
Solution 2:
Worked on my machine python 2.7.10
('This variable is callable', 1)
('And this one is also callable', 2)
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Post a Comment for "Multiprocessing Error With Results"