0
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.

並列処理がJupyterLabでエラーとなった

Last updated at Posted at 2021-09-26

並列処理をJupyterLabでやるとエラーとなったのでその解決までの手順を記録します

参考講座:books::pencil2:

こちらの講座を参考にしています!

##今回のエラー内容:scream:

from multiprocessing import Pool, cpu_count
import time

p = Pool(processes=cpu_count())
# sec秒間停止する関数を作成
def wait_sec(sec):
    time.sleep(sec)
    return sec ** 2

before = time.time()
results = p.map(wait_sec, [1, 5, 3])
after = time.time()
print('it took {} sec'.format(after - before))

これをJupyterLabで実行したら、

Process ForkPoolWorker-1:
Process ForkPoolWorker-2:
Process ForkPoolWorker-3:
Traceback (most recent call last):
Traceback (most recent call last):
  File "/opt/anaconda3/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
Traceback (most recent call last):
  File "/opt/anaconda3/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
  File "/opt/anaconda3/lib/python3.7/multiprocessing/process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "/opt/anaconda3/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
  File "/opt/anaconda3/lib/python3.7/multiprocessing/pool.py", line 110, in worker
    task = get()
  File "/opt/anaconda3/lib/python3.7/multiprocessing/process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "/opt/anaconda3/lib/python3.7/multiprocessing/queues.py", line 354, in get
    return _ForkingPickler.loads(res)
  File "/opt/anaconda3/lib/python3.7/multiprocessing/process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "/opt/anaconda3/lib/python3.7/multiprocessing/pool.py", line 110, in worker
    task = get()
AttributeError: Can't get attribute 'wait_sec' on <module '__main__'>
  File "/opt/anaconda3/lib/python3.7/multiprocessing/pool.py", line 110, in worker
    task = get()
  File "/opt/anaconda3/lib/python3.7/multiprocessing/queues.py", line 354, in get
    return _ForkingPickler.loads(res)
  File "/opt/anaconda3/lib/python3.7/multiprocessing/queues.py", line 354, in get
    return _ForkingPickler.loads(res)
AttributeError: Can't get attribute 'wait_sec' on <module '__main__'>
AttributeError: Can't get attribute 'wait_sec' on <module '__main__'>

というとてつもない量のwarningが出て、30秒待っても実行が終わらないので調べてみるとフリーズしてることに気づく

試行錯誤:robot:

調べるとメモリがいっぱいでフリーズしている可能性があるらしい
・ゴミ箱を空にする
・デスクトップにを整理する
・不必要なアプリ、JupyterLab使わないnotebookを、停止する
・Macを再起動する(これやったらDockerがExitになってそれの解決法を探すのに時間かかっってしまった笑
※解決方法は簡単だった)

結果は変わらなかった

試行錯誤2:robot:

そもそもJupyterLabで並列処理できない場合があるらしい
VScodeでやってみる

####うまくいかない
if name == "main"を使って欲しいとエラーが出てきた

やっと解決!:relaxed:

from multiprocessing import Pool,cpu_count
import time
 
def wait_sec(sec):
    time.sleep(sec)
    return sec**2
 
if __name__ == "__main__":
    p = Pool(processes=cpu_count()-1)
    before = time.time()
    #並列処理
    results = p.map(wait_sec,[1,5,3])
    after = time.time()
    print("It took {}".format(after - before))
    p.close()
    p.join()
    print(results)

このコードでうまく実行することができた!

0
0
1

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
0
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?