Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage Question: Scheduling tasks within a fiber without spawning a new thread #312

Open
sheldonkreger opened this issue Jun 20, 2018 · 1 comment

Comments

@sheldonkreger
Copy link

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.

https://github.com/puniverse/quasar/blob/master/quasar-actors/src/test/java/co/paralleluniverse/actors/ActorTest.java#L185

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?

@doctorpangloss
Copy link

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:

return timer.schedule(fiber, blocker, delay, unit);

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants