Usage Question: Scheduling tasks within a fiber without spawning a new thread
sheldonkreger opened this issue · 1 comments
I'm looking for a way to periodically execute a function inside a ServerActor. This should continue indefinitely until the ServerActor is terminated. In Java, this is typically done by creating a new thread and running a timer. Even in the Quasar tests for Actor, sleep() is used to cause a delay between function calls, which of course creates a new thread.
Coming from the Erlang/Elixir world, I have been happy with an implementation like this, where the GenServer passes itself a message periodically.
https://stackoverflow.com/a/32097971/1289597
My concern is that if I instantiate ServerActors using spawn() rather than spawnThread(), each instance will run in a fiber. However, if I put a sleep() inside one of the methods to handle scheduling, I now have both a fiber and a thread associated with this ServerActor. This seems risky in regard to scaling.
Is there a standard way to implement lightweight task scheduling using fibers in Quasar?
Even in the Quasar tests for Actor, sleep() is used to cause a delay between function calls, which of course creates a new thread.
In that code, it's used to just prevent the actor from dequeueing both message uninterrupted.
I now have both a fiber and a thread associated with this ServerActor
Sort of.
When you call Strand.sleep
in a fiber, you'll eventually schedule the fiber to be unparked by this:
That's it!
The state of the timer ultimately is stored in your fiber scheduler instance. You can also create a "timer actor" that sends messages at the right period.