Frequently Asked Questions

Hinrik Örn Sigurðsson edited this page Sep 12, 2013 · 12 revisions

Q: My program written with Celluloid just exits when I run it. What gives?

A: If you build a program entirely with Celluloid actors that are running in their own threads, there may be nothing for Ruby's main thread to do. However, the way Ruby is designed, if the main thread exits, all the background threads exit.

The simplest thing to do is to simply sleep the main thread, which allows all of the Celluloid actors to continue to run:

sleep

You can also run one of your actors in the foreground by calling the run method on its class, passing it all the arguments you'd ordinarily pass to new:

MyClass.run

However, a better solution is to leverage Celluloid's supervision tree feature, and build a [Celluloid::SupervisionGroup](Supervision Groups) whose watchdog you can run in the foreground. Instead of just sleeping the main thread, you can define the toplevel group of a supervision tree as a class, then do:

MyGroup.run

which will run the toplevel supervisor for your application in the main thread.

Q: How do I silence the logger?

A: Celluloid.logger = nil (although you should probably at least consider logging to a file)

See also Logging.

Q: How do I define a finalizer for an actor?

A: finalizer :time_to_go in your Actor class will make Celluloid call that instance method on your actor during shutdown.

See also Finalizers.

Q: How can I use a custom exception notifier like Airbrake?

A: Celluloid.exception_handler { |ex| MyNotifier.notify(ex) }

Q: How can I raise an exception in the caller without crashing the receiver?

A: abort MyException.new("you f'd up, not me!")

Q: I'm trying to create 2000+ actors on OS X and I'm getting a "Cannot allocate memory" error. What do I do?

A: Install Linux, problem solved? ;) Seriously though, OS X has some built-in limitations on how many threads you can create and there's no workaround available at present, sorry.

Q: Can I do blocking I/O inside an actor? (or do I have to use Celluloid::IO)

A: YES, you can do blocking I/O (or any other type of blocking operation) inside an actor, even if it requires native extensions (e.g. DB drivers). All actors run inside their own thread, so making blocking calls will only block that actor, not every actor in the system. Once the blocking I/O operation completes, the actor will resume processing its mailbox, and respond to any pending requests. However, you should still avoid making indefinite blocking calls, e.g. accepting from a listener socket, without using Celluloid::IO, as this can potentially lead to deadlocks. Celluloid::IO is also useful if you want to service many sockets with a single actor.

Q: What does a FiberStackError or SystemStackError mean?

A: Please see the notes on FiberStackErrors