2017-03-06 7 views
0

スレッド間でマルチプロセッシングのスワップが速くなっているため、スワッピングを開始しましたが、予期しない結果が発生しています。これは、スレッドが以前になかったときに私のスクリプト全体が何度もループする原因になります。Pythonマルチプロセッシングでループ全体が発生する

スニペット例:

stuff_needs_done = true 
more_stuff_needs_done = true 
print "Doing stuff" 

def ThreadStuff(): 
    while 1 == 1: 
    #do stuff here 

def OtherThreadStuff(): 
    while 1 == 1: 
    #do other stuff here 

if stuff_needs_done == true: 
    Thread(target=ThreadStuff).start() 

if more_stuff_needs_done == true: 
    Thread(target=OtherThreadStuff).start() 

私が期待するように、これは動作します。スレッドは停止するまで開始して実行されます。しかし、これらの多くを実行するとオーバーヘッドが高くなります(私は言われています)ので、私はマルチプロセッシングに交換しようとしました。

スニペット例は:「ものをやって、」出力が立ち上がるとスレッドのカップルを実行して

stuff_needs_done = true 
more_stuff_needs_done = true 
print "Doing stuff" 


def ThreadStuff(): 
    while 1 == 1: 
    #do stuff here 

def OtherThreadStuff(): 
    while 1 == 1: 
    #do other stuff here 

if stuff_needs_done == true: 
    stuffproc1= Process(target=ThreadStuff).start() 

if more_stuff_needs_done == true: 
    stuffproc1= Process(target=OtherThreadStuff).start() 

しかし、何が起こるようですが、全部では数回を開始します。

私はいくつかの.join()を入れることができますが、印刷出力が再び実行されるループが存在しません。

私の希望はこれだけの文法ですが、なぜスクリプト全体がループするのかを調べようとしています。私は本当に正しい方向へのポインタを感謝します。

+0

あなたは、ほぼ各ラインで構文エラーがあります。あなたの投稿を修正してください。 –

答えて

1

これはdocsに記載されている:

Safe importing of main module

Make sure that the main module can be safely imported by a new Python interpreter without causing unintended side effects (such a starting a new process).

For example, under Windows running the following module would fail with a RuntimeError:

from multiprocessing import Process 

def foo(): 
    print 'hello' 

p = Process(target=foo) 
p.start() 

Instead one should protect the “entry point” of the program by using if __name__ == '__main__' : as follows:

from multiprocessing import Process, freeze_support 

def foo(): 
    print 'hello' 

if __name__ == '__main__': 
    freeze_support() 
    p = Process(target=foo) 
    p.start() 

This allows the newly spawned Python interpreter to safely import the module and then run the module’s foo() function.

+0

パーフェクトありがとう! – PoweredByCoffee

関連する問題