scala - Pattern for interruptible loops using actors -
i'm designing actor consumes items endless stream, , need way control when starts , stops using messages. there common pattern implementing interruptible loops actors? thinking of having actor send messages itself. (pseudo scala):
class interruptible extends actor { val stream: stream val running: boolean def receive = { case "start" => { running = true consumeitem } case "stop" => { running = false } case "consumenext" => consumeitem } def consumeitem { if (running) { stream.getitem ! "consumenext" } } } is best way go things?
thanks!
perhaps encoded this:
class interruptible extends actor { val stream: stream def inactive: receive = { // behavior when inactive case "start" => self become active } def active: receive = { // behavior when it's active case "stop" => self become inactive case "next" => dosomethingwith(stream.getitem) self ! "next" } def receive = inactive // start out inactive } cheers,
Comments
Post a Comment