8

Assume that a ray actor is defined as below

@ray.remote
class Buffer:
    def __init__(self):
        self.memory = np.zeros(10)

    def modify_data(self, indices, values):
        self.memory[indices] = values

    def sample(self, size):
        indices = np.random.randint(0, 10, size)
        return self.memory[indices]

Is it thread-safe to have other actors call methods of Buffer without any lock?

Maybe
  • 2,129
  • 5
  • 25
  • 45
  • 2
    If you are ensuring that you are executing this on different objects in each thread, then should be fine. – user5214530 Dec 24 '19 at 07:14
  • 1
    Hi, thanks for answering. I'm sorry that I don't understand what do you mean by `executing this on different objects in each thread`. For your reference, I don't explicitly define any additional thread to manipulate this object, and only ray actors are involved here. – Maybe Dec 24 '19 at 07:23
  • 1
    I've just meant that in case you are processing (i.e.) some common file or resource in multiple threads, that would be a problem, but if you are instancing the class as-it-is in here, I would say you wont have the problems. – user5214530 Dec 24 '19 at 07:46

1 Answers1

6

Yes; by default, only one method will execute on a Ray actor at a time. Ordering from concurrent calls is not guaranteed.

With Ray 0.8, you'll be able to set ActorClass.options(max_concurrency=N) to override this serial execution guarantee.

richliaw
  • 1,925
  • 16
  • 14
  • Hi, for example, Ray's queue was implemented with `asyncio.Queue`. `asyncio.Queue` is not thread-safe: https://docs.python.org/3/library/asyncio-queue.html. Is Ray's queue still thread-safe? – GoingMyWay Jun 09 '22 at 15:48