今天我写了这篇文章Multiprocessing Error with Results。无结构多处理打印命令
现在我修改这个脚本:
import multiprocessing
class class1():
def classfunction1(self, a):
self.x = a
print("class 1")
class class2():
def classfunction2(self, a):
self.y = a
print("class 2")
def test(i):
print("I'm in the Testfunction")
b = i * class1.x * class2.y
return b
def init(): # added
print("I'm in the Initfunction")
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)
我插入一些打印命令的类。但结果是无结构的印刷,如:
I'm in the Initfunction
class 1
class 2
This variable is callable 1
And this one is also callable 2
I'm in the Initfunction
class 1
class 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
I'm in the Initfunction
class 1
class 2
I'm in the Initfunction
class 1
class 2
I'm in the Initfunction
class 1
class 2
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
在我只想一旦类印刷品......只有文本“我在Testfunction”我要多(10次)。
有没有人可能的解决方案?
你能举个例子吗?我不明白你是怎么意思的...... – John28