ruby-concurrency / ruby-concurrency/concurrent-ruby

Waiting for Actor termination?

Open
#825 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
Ruby
Stars
5.8k
Forks
420
Avg merge
20h 45m
Merged PRs (30d)
4

Description

Is there an easy way to wait for an Actor to terminate? I'm aware that I can wait till all messages are processed:

actor = MyActor.spawn(name: 'actor')
actor << :dostuff
actor.ask(:await).wait

or wait for the actor to respond (which AFAIK is the same)

actor = MyActor.spawn(name: 'actor')
actor << :dostuff
actor.ask(:some_result).value # or actor.ask!(:some_result)

but both are not what I'm looking for. I want to know how to block until an actor is terminated when the actor has asynchronous execution itself:

class MyActor < Concurrent::Actor::Context
  def on_message(message)
    case message
    when :start
      promises = 10.times.map do |n|
        Concurrent::Promises.future do
          sleep 1
          n
        end
      end

      Concurrent::Promises.zip(*promises).then do
        self.tell :terminate!
      end

      Concurrent::Actor::Behaviour::MESSAGE_PROCESSED
    else
      pass
    end
  end
end

my_actor = MyActor.spawn(name: 'actor')
my_actor.tell(:start)

# How to do this, as this is obviously not great:
loop do
  break if my_actor.ask!(:terminated?)
end

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at Concurrent::Actor::Context and the actor lifecycle messages shown in the issue, then trace how termination state is currently exposed. No file or test is named; done means providing a non-polling way to wait for completion after asynchronous work finishes, with coverage for the future-based example.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.