[Python] Dealing with multiple call errors in ray.init

environment

python

Error that occurred

(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()'.

About ignore_reinit_error

ignore_reinit_error can decide whether to issue __error __ when __ray.init () __ is executed __ multiple times __.

The default __ is False.

Example

Take the following programming as an example.

Example 1: Example of changing ignore_reinit_error to __ False </ font> __. That is, it remains ray.init (num_cpus = 1).

python


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()'.                      

In Example 1 above, ray.init has been executed ___2 times, so error has occurred.

Example 2: An example where ignore_reinit_error is changed to __ True </ font> __. That is, ray.init (num_cpus = 1, ignore_reinit_error = True).

python


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                    

In Example 2, ray.init has been executed ___2 times, but no __ error has occurred __.

The above is the operation of ignore_reinit_error.

Recommended Posts