今日はこの投稿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]
プリント...テキストのみは、私は複数(10回)したい「私はTestfunctionにいますよ」。
解決策はありますか?
サンプルコードを教えてもらえますか?私はあなたの意味を正確に理解していません... – John28