# Is it possible to use Ray in a subprocess created with multiprocessing.Process?

**URL:** https://discuss.ray.io/t/is-it-possible-to-use-ray-in-a-subprocess-created-with-multiprocessing-process/7966
**Category:** Ray Core
**Created:** [October 20, 2022, 2:47am UTC](https://discuss.ray.io/t/is-it-possible-to-use-ray-in-a-subprocess-created-with-multiprocessing-process/7966 "2022-10-20T02:47:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Hanyu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/hanyu/32/3010_2.png) [@Hanyu](https://discuss.ray.io/u/Hanyu)
#### Post date: [October 20, 2022, 2:47am UTC](https://discuss.ray.io/t/is-it-possible-to-use-ray-in-a-subprocess-created-with-multiprocessing-process/7966/1 "2022-10-20T02:47:25Z")

</div>

**How severe does this issue affect your experience of using Ray?**

- Medium: It contributes to significant difficulty to complete my task, but I can work around it.

Hi,

I wonder if it is possible to use Ray (for example, call ray.get(), or create an Actor) from a subprocess created with multiprocessing.Process of the driver script.  
I tested it myself, and the code got stuck when creating the Actor.  
Environment: Python 3.8, Ray 2.0.0.

```auto
import ray
import multiprocessing

ray.init()

@ray.remote
class Foo:
    def __init__ (self, a):
        self.a = a
    def p(self):
        return self.a

def create_and_run(a):
    print("before creating")
    foo = Foo.remote(a)
    print("after creating")
    result = ray.get(foo.p.remote())
    print(result)

p = multiprocessing.Process(target=create_and_run, args=(1,))
p.start()
p.join()

```

Output:

```auto
root@iZ2zeinq87v01lgdaj1xdiZ:/home/hanyu/ray_tests# python multiprocess.py
2022-10-20 02:35:25,176 INFO worker.py:1333 -- Connecting to existing Ray cluster at address: 172.20.5.216:6379...
2022-10-20 02:35:25,182 INFO worker.py:1509 -- Connected to Ray cluster. View the dashboard at http://127.0.0.1:8265
before creating

```

If I create the Actor in the main process, and call ray.get() in the subprocess (as shown below), the code got stuck at the ray.get().

```auto
import ray
import multiprocessing

ray.init()

@ray.remote
class Foo:
    def __init__ (self, a):
        self.a = a
    def p(self):
        return self.a

def run(foo):
    print("before result")
    result = foo.p.remote()
    print("before get")
    print(ray.get(result))

foo = Foo.remote(1)
p = multiprocessing.Process(target=run, args=(foo,))
p.start()
p.join()

```

Output:

```auto
root@iZ2zeinq87v01lgdaj1xdiZ:/home/hanyu/ray_tests# python multiprocess.py
2022-10-20 02:40:06,906 INFO worker.py:1333 -- Connecting to existing Ray cluster at address: 172.20.5.216:6379...
2022-10-20 02:40:06,912 INFO worker.py:1509 -- Connected to Ray cluster. View the dashboard at http://127.0.0.1:8265
before result
before get

```

Thank you!

---

<div class="post-metadata">

### Author: ![sangcho](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sangcho/32/425_2.png) [@sangcho](https://discuss.ray.io/u/sangcho)
#### Post date: [October 20, 2022, 9:56am UTC](https://discuss.ray.io/t/is-it-possible-to-use-ray-in-a-subprocess-created-with-multiprocessing-process/7966/2 "2022-10-20T09:56:43Z")

</div>

We don’t recommend using Ray this way. Ray doesn’t work well with fork because 1. Ray processes are supposed to be managed by Ray systems, but if you fork the process, they may not be managed by Ray properly. 2. Ray uses gRPC internally, and it doesn’t play well with forking (there’s a way to make gRPC play well with fork though). This usage pattern is also not tested and behaviors are undefined. What’s the usecase btw?

---

<div class="post-metadata">

### Author: ![Hanyu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/hanyu/32/3010_2.png) [@Hanyu](https://discuss.ray.io/u/Hanyu)
#### Post date: [October 26, 2022, 8:57am UTC](https://discuss.ray.io/t/is-it-possible-to-use-ray-in-a-subprocess-created-with-multiprocessing-process/7966/3 "2022-10-26T08:57:45Z")

</div>

I see. Thanks for the prompt reply! I was using some strange legacy code that led me to such a weird situation. Never mind 🤣
