1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python PyInstallerでexeファイル作成 並列処理だとPCが固まる

Posted at

Pythonの並列処理をexeで実行したらPCが固まる

実行環境

・Windodws10
・Python 3.8.3
・PyInstaller 3.6

コンソールから実行するとなんてことない並列処理が PyInstallerでexeファイルにしてクリックすると、 exeのプロセスが超たくさん立ち上がり、PCがフリーズしてしまう。絶望するくらい

※PyInstallerがPython3.8.3に対応していないのが原因かと思いきや今回の現象には関係ありませんでした。2020/5/21現在

結論

freeze_support() が必要

exeファイルで実行するとPCが固まるPython並列処理のサンプル

並列処理サンプル

from multiprocessing import Pool 

##### この場合は,引数の二乗を返す関数 ###
def nijou(x):
    print( x*x )
 
###### 並列計算させてみる #########
if __name__ == "__main__":
    p = Pool(4)
    p.map( nijou, range(10) )

freeze_support()の記述が必要だった

メインモジュールの if name == 'main' の直後にこの関数を呼び出す必要があります
freeze_support() の行がない場合、フリーズされた実行可能形式を実行しようとすると RuntimeError
が発生するとのこと
freeze_support() の呼び出しは Windows 以外の OS では効果がありません。
さらに、もしモジュールが Windows の通常の Python インタプリタによって実行されているならば(プログラムがフリーズされていなければ) freeze_support() は効果がありませんとのこと

参考はここ・multiprocessing プロセスベースの並列処理

※とりあえずできたからよいのですが、フリーズされた実行形式とはそもそもなんでしょうか?
わからないです。わかるかた教えてください。
exeファイルのことかしら。

exeで実行しても固まらない

from multiprocessing import Pool,freeze_support
# freeze_supporを追加

##### この場合は,引数の二乗を返す関数 ###
def nijou(x):
    print( x*x )
 
###### 並列計算させてみる #########
if __name__ == "__main__":
    freeze_support() #追加しました
    p = Pool(4)
    p.map( nijou, range(10) )

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?