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
class class1():
def classfunction1(self, a):
self.x = a
class class2():
def classfunction2(self, a):
self.y = a
def test(i):
print("I'm in the Testfunction")
b = i * class1.x * class2.y
return b
def init(): # added
global class1, class2
class1 = class1()
class2 = class2()
x = 1
y = 2
class1.classfunction1(x)
class2.classfunction2(y)
if __name__ == "__main__":
init() # explicit call here
print("This variable is callable", class1.x)
print("And this one is also callable", class2.y)
counter = []
for i in range(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"