2016-12-06 3 views
0

今日はこの投稿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にいますよ」。

解決策はありますか?

+0

サンプルコードを教えてもらえますか?私はあなたの意味を正確に理解していません... – John28

答えて

1

私は(悪い)コメント(現在削除)で説明しようとしたとして、あなたは(とクラスメソッドに)init()関数にデフォルト値とオプションの引数を追加することによってそれを行うことができます。

from __future__ import print_function 
import multiprocessing 
import sys 

sys_print = print # save built-in print function before replacement is defined 

def print(*args, **kwargs): 
    """Replacement for built-in that flushes output stream after each call.""" 
    sys_print(*args, **kwargs) 
    stdout = kwargs.get('file', sys.stdout) 
    stdout.flush() # force any buffered output to be displayed 

class class1(): 
    # note addition of optional argument with default value 
    def classfunction1(self, a, notify=False): 
     self.x = a 
     if notify: print("class 1") 

class class2(): 
    # note addition of optional argument with default value 
    def classfunction2(self, a, notify=False): 
     self.y = a 
     if notify: print("class 2") 

def test(i): 
    print("I'm in the Testfunction") 
    b = i * class1.x * class2.y 

    return b 

def init(notify=False): # note addition of optional argument with default value 
    if notify: print("I'm in the Initfunction") 
    global class1, class2 
    class1 = class1() 
    class2 = class2() 
    x = 1 
    y = 2 
    class1.classfunction1(x, notify) 
    class2.classfunction2(y, notify) 

if __name__ == "__main__": 
    init(True) # override default arg in this explicit call 
    print("This variable is callable", class1.x) 
    print("And this one is also callable", class2.y) 
    counter = list(range(10)) 
    pool = multiprocessing.Pool(initializer=init, processes=4) # implicit calls 
    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 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] 
+0

FYI:あなたが望むようにクラスを "呼び出し可能"にする別の方法は、['__call__()'](https://docs.python.org/3 /reference/datamodel.html#object.__call__)メソッドを使用します。これは、クラスを自分自身のインスタンス(これを行うために現在使用している 'class1 = class1()'テクニックで置き換える方が望ましいでしょう)。 – martineau

+0

@martinuea: "この変数は呼び出し可能です"と "これも呼び出し可能です"というテキストがTestfunction-printとクラスプリントの後に出力される理由は分かりません。マルチプロセッシングが開始される前のコマンドです。そして、Testfunction-printの前にclass1とclass2を取得するためには、何を変更する必要がありますか?私が期待するシーケンスは、 "This variable .."と "And this one ..."、 "Initfunction"、 "class1" "class2"、 "Testfunction"、少なくとも変数結果の数字です。 – John28

+0

出力の奇妙な順序は、複数の異なるプロセスが同時に実行されているために、同じ共有コンソールに複数のものを同時に印刷しようとするためです。私はそれが厄介なものではないことに驚いています... – martineau

関連する問題