# How to accomplish this thing

**URL:** <https://discuss.ray.io/t/how-to-accomplish-this-thing/5209>\
**Category:** Ray Core\
**Created:** [February 27, 2022, 6:19am UTC](https://discuss.ray.io/t/how-to-accomplish-this-thing/5209 "2022-02-27T06:19:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marsupialtail](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/marsupialtail/32/1950_2.png) [@marsupialtail](https://discuss.ray.io/u/marsupialtail)\
**Post date:** [February 27, 2022, 6:19am UTC](https://discuss.ray.io/t/how-to-accomplish-this-thing/5209/1 "2022-02-27T06:19:03Z")

</div>

Want some advice on implementing the following behavior.

I have an Actor that continuously push some data to a bunch of IP addresses. Currently it is implemented as a while loop inside of a remote method “execute”.

At some point, I would like to invoke another remote method “update” that changes one of those IP addresses. Then I would want this change to be reflected _during_ the execution of “execute”.

What is the canonical way of accomplishing this thing in Ray? Do I have to break up the “execute” method?

It appears I can use a threaded actor, and try to invoke execute in one thread and invoke update in the other thread and changing the shared state. This seems a very dangerous thing to do. Is there more documentation on how shared state is managed between threads in a threaded actor?

---

<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:** [February 28, 2022, 3:50pm UTC](https://discuss.ray.io/t/how-to-accomplish-this-thing/5209/2 "2022-02-28T15:50:03Z")

</div>

Since Ray actor can only run 1 task at a time, if you have the method that contains while loop (e.g., your `execute` method), it cannot run other methods.

But we do have a way to workaround it.

> It appears I can use a threaded actor, and try to invoke execute in one thread and invoke update in the other thread and changing the shared state. This seems a very dangerous thing to do. Is there more documentation on how shared state is managed between threads in a threaded actor?

This is the working solution. But you need to make sure all IP addresses variables are protected by the lock. Ray doesn’t protect any states when you use the threaded actors (it is the same model as regular multi threading in python).

Alternatively, you can use the async actor.

```python3
class A:
    async def execute(self):
        while True:
           # do what you want
           await asyncio.sleep(0) # This will allow to context switch and execute other actor method

    async def update(self, ip):
        # do something

```

---

<div class="post-metadata">

**Author:** ![marsupialtail](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/marsupialtail/32/1950_2.png) [@marsupialtail](https://discuss.ray.io/u/marsupialtail)\
**Post date:** [March 1, 2022, 4:42am UTC](https://discuss.ray.io/t/how-to-accomplish-this-thing/5209/3 "2022-03-01T04:42:13Z")

</div>

In an async actor, how does it work if I call update while execute is running? Will update run when execute hits await?

---

<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:** [March 1, 2022, 10:11am UTC](https://discuss.ray.io/t/how-to-accomplish-this-thing/5209/4 "2022-03-01T10:11:05Z")

</div>

Whenever you call `await asyncio.sleep(0)`, this yields control to the next queued item in the event loop, which in this case is update.
