Linux ではfork
、子のスポーンに使用されるため、コピー オン ライト セマンティクスを使用して、親のグローバル スコープ内にあるものはすべて子でも使用できます。
Windows では、親プロセスimport
のモジュールでモジュール レベルにあるものはすべて__main__
、子プロセスに再インポートされます。
これは、次のような親モジュール ( と呼びましょうsomeModule
) がある場合を意味します。
import someOtherModule
import concurrent.futures
def foo(str):
x = someOtherModule.fooBar()
if __name__ == "__main__":
with concurrent.futures.ProcessPoolExecutor(max_workers=settings.MAX_PROCESSES) as executor:
for stuff in executor.map(foo, paths):
# stuff
次someOtherModule
のようになります。
myHat='green'
def fooBar():
return myHat
この例でsomeModule
は、__main__
がスクリプトのモジュールです。したがって、Linux では、myHat
子で取得するインスタンスは、 のインスタンスのコピー オン ライト バージョンになりsomeModule
ます。Windows では、各子プロセスはsomeModule
ロードされるとすぐに再インポートされ、その結果、someOtherModule
再インポートも行われます。
Gateway
これがあなたが望む動作であるかどうかを確信できるかどうかを判断するのに十分なpy4jオブジェクトについて私は知りません。オブジェクトがピクル可能な場合、Gateway
代わりに各子に明示的に渡すことができますが、multiprocessing.Pool
代わりにa を使用する必要がありconcurrent.futures.ProcessPoolExecutor
ます。
import someOtherModule
import multiprocessing
def foo(str):
x = someOtherModule.fooBar()
def init(hat):
someOtherModule.myHat = hat
if __name__ == "__main__":
hat = someOtherModule.myHat
pool = multiprocessing.Pool(settings.MAX_PROCESSES,
initializer=init, initargs=(hat,))
for stuff in pool.map(foo, paths):
# stuff
ただし、ユースケースのためにこれを行う必要があるようには見えません。おそらく、再インポートを使用しても問題ありません。