0
1

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】ray.init での多重呼出しエラー対処

Last updated at Posted at 2020-10-15

#環境
python

#発生したエラー

(ignore_reinit_error=True), RuntimeError: Maybe you called ray.init twice by accident? This error can be suppressed by passing in 'ignore_reinit_error=True' or by calling 'ray.shutdown()' prior to 'ray.init()'.

#ignore_reinit_errorについて
__ignore_reinit_error__は__ray.init()__が__複数_回実行__されたときに__エラー__を出すか出さないかを決めることができる。

__デフォルト__では__False__になっている。

#例
例として以下のプログラミングを見てみる。

例1 : ignore_reinit_error__をFalse__にした例。
つまり、ray.init(num_cpus=1)のまま。

import time
import ray


if __name__ == "__main__":
    
    for i in range(2):
        ray.init(num_cpus=1)

        @ray.remote
        def time_function(num):
            time.sleep(1)
            return num

        time.sleep(2)

        start_time = time.time()

        numbers = ray.get([time_function.remote(i) for i in range(5)])

        end_time = time.time()
        duration = end_time-start_time

        print('numbers = {}, duration = {}'.format(numbers, duration))

#RuntimeError: Maybe you called ray.init twice by accident? This error can be suppressed by passing in 'ignore_reinit_error=True' or by calling 'ray.shutdown()' prior to 'ray.init()'.                      

上の例1では__ray.init__が__2回実行__されているので__エラー__が起きている。

例2 : ignore_reinit_error__をTrue__にした例。
つまり、ray.init(num_cpus=1, ignore_reinit_error=True)。

import time
import ray


if __name__ == "__main__":
    
    for i in range(2):
        ray.init(num_cpus=1, ignore_reinit_error=True)

        @ray.remote
        def time_function(num):
            time.sleep(1)
            return num

        time.sleep(2)

        start_time = time.time()

        numbers = ray.get([time_function.remote(i) for i in range(5)])

        end_time = time.time()
        duration = end_time-start_time

        print('numbers = {}, duration = {}'.format(numbers, duration))

#numbers = [0, 1, 2, 3, 4], duration = 5.047511100769043
Calling ray.init() again after it has already been called.
numbers = [0, 1, 2, 3, 4], duration = 5.015073537826538                    

例2では__ray.init__が__2回実行__されていても__エラーが起きていない__。

以上がignore_reinit_errorの動作である。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?