# Multiprocess safe Shared object

**URL:** <https://discuss.ray.io/t/multiprocess-safe-shared-object/3613>\
**Category:** Ray Core\
**Created:** [September 23, 2021, 11:15pm UTC](https://discuss.ray.io/t/multiprocess-safe-shared-object/3613 "2021-09-23T23:15:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@daniel](https://discuss.ray.io/u/daniel)\
**Post date:** [September 23, 2021, 11:15pm UTC](https://discuss.ray.io/t/multiprocess-safe-shared-object/3613/1 "2021-09-23T23:15:19Z")

</div>

How to define a multiprocess safe shared object class in ray? should I use `lock`?

---

<div class="post-metadata">

**Author:** ![yic](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/yic/32/437_2.png) [@yic](https://discuss.ray.io/u/yic)\
**Post date:** [September 24, 2021, 3:22am UTC](https://discuss.ray.io/t/multiprocess-safe-shared-object/3613/2 "2021-09-24T03:22:24Z")

</div>

Sorry, I don’t get what you mean here. Could you give more details? For example, why do you think it’s causing anything difference in ray?

---

<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:** [September 24, 2021, 9:49pm UTC](https://discuss.ray.io/t/multiprocess-safe-shared-object/3613/3 "2021-09-24T21:49:08Z")

</div>

Ray objects are immutable, so you don’t need to think about “shared writing to one object”. If you’d like to modify objects in place, you can use an actor instead. For example, put the object to the actor, and write actor methods to modify them

---

<div class="post-metadata">

**Author:** ![daniel](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@daniel](https://discuss.ray.io/u/daniel)\
**Post date:** [September 24, 2021, 10:09pm UTC](https://discuss.ray.io/t/multiprocess-safe-shared-object/3613/4 "2021-09-24T22:09:30Z")

</div>

Does this mean the function (to update an actor local variable) of an Actor `A` in a ray can only be called by one another actor `B`?

even if there are two other actors (`B` and `C`) are trying to concurrently invoke the A’s internal value update it will be scheduled to be conducted sequentially by ray so that there is no data consistency issue.

---

<div class="post-metadata">

**Author:** ![yic](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/yic/32/437_2.png) [@yic](https://discuss.ray.io/u/yic)\
**Post date:** [September 24, 2021, 10:12pm UTC](https://discuss.ray.io/t/multiprocess-safe-shared-object/3613/5 "2021-09-24T22:12:55Z")

</div>

Yes, all actor methods are run sequentially. No race condition in this case.

---

<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:** [September 29, 2021, 7:34am UTC](https://discuss.ray.io/t/multiprocess-safe-shared-object/3613/6 "2021-09-29T07:34:47Z")

</div>

A couple more things to note;

1. If the actor method is invoked by the same caller, the ordering is guaranteed by Ray.
2. When you “get” the modified object, I recommend you to implement returning the parts of objects instead of returning the whole object. The zero copy read might not work in this case.
