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?
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.
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