hemant
2007-May-03 22:38 UTC
[Eventmachine-talk] what would be simplest way to remove a timer,
There are ways to get the above said behaviour with add_timer, but that isn''t so convenient.
Francis Cianfrocca
2007-May-04 01:02 UTC
[Eventmachine-talk] what would be simplest way to remove a
On 5/4/07, hemant <gethemant at gmail.com> wrote:> > There are ways to get the above said behaviour with add_timer, but > that isn''t so convenient. > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >Ick. This isn''t such a nice question. Today, EventMachine#add_timer and EventMachine#add_periodic_timer have no meaningful return value. What if we define a new class, EventMachine::Timer, and have the two add-timer functions return an instance of the new class? The class would have a #cancel method. Would that work? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20070504/dad5934a/attachment-0001.html
On 5/4/07, Francis Cianfrocca <garbagecat10 at gmail.com> wrote:> On 5/4/07, hemant <gethemant at gmail.com> wrote: > > There are ways to get the above said behaviour with add_timer, but > > that isn''t so convenient. > > _______________________________________________ > > Eventmachine-talk mailing list > > Eventmachine-talk at rubyforge.org > > http://rubyforge.org/mailman/listinfo/eventmachine-talk > > > > > > Ick. This isn''t such a nice question. > > Today, EventMachine#add_timer and EventMachine#add_periodic_timer have no > meaningful return value. What if we define a new class, EventMachine::Timer, > and have the two add-timer functions return an instance of the new class? > The class would have a #cancel method. > > Would that work? > > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >Sounds like a plan. -- gnufied
Francis Cianfrocca
2007-May-04 04:10 UTC
[Eventmachine-talk] what would be simplest way to remove a
On 5/4/07, hemant <gethemant at gmail.com> wrote:> > Sounds like a plan.Cancelling the periodic timer is a bit of a pain, as you surmised. I''ll write back here when I decide how to handle this. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20070504/f34f628b/attachment.html
Francis Cianfrocca
2007-May-04 05:18 UTC
[Eventmachine-talk] what would be simplest way to remove a
On 5/4/07, hemant <gethemant at gmail.com> wrote:> > On 5/4/07, Francis Cianfrocca <garbagecat10 at gmail.com> wrote: > > On 5/4/07, hemant <gethemant at gmail.com> wrote: > > > There are ways to get the above said behaviour with add_timer, but > > > that isn''t so convenient. > > > _______________________________________________ > > > Eventmachine-talk mailing list > > > Eventmachine-talk at rubyforge.org > > > http://rubyforge.org/mailman/listinfo/eventmachine-talk > > > > >Ok, try this. Sync to the latest revision. There are two new classes, EventMachine::Timer and EventMachine::PeriodicTimer. You can instantiate either one at any time while EventMachine is running. Their constructors take the same parameters as EventMachine#add_timer and EventMachine#add_periodic_timer, respectively. Both have a cancel method. For the Periodic flavor, you can cancel it within its own handler as well as outside of it. EventMachine.run { timer = EventMachine::Timer.new(10) {puts "Timer Fired"} EventMachine::Timer.new(5) {timer.cancel} # The 10-second timer will never fire. pt = EventMachine::PeriodicTimer.new(1) {puts Time.new} EventMachine::Timer.new(5) {pt.cancel} # pt will fire once a second for five seconds. n = 6 pt1 = EventMachine::PeriodicTimer.new(1) { puts "n=#{n}" n -= 1 pt1.cancel if n == 0 #Timer cancels itself after firing six times. } } Let me know if you like it. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20070504/7b1c1451/attachment.html
> > Ok, try this. > Sync to the latest revision. There are two new classes, EventMachine::Timer > and EventMachine::PeriodicTimer. > > You can instantiate either one at any time while EventMachine is running. > Their constructors take the same parameters as EventMachine#add_timer and > EventMachine#add_periodic_timer, respectively. Both have a > cancel method. For the Periodic flavor, you can cancel it within its own > handler as well as outside of it. > > EventMachine.run { > timer = EventMachine::Timer.new(10) {puts "Timer Fired"} > EventMachine::Timer.new(5) {timer.cancel} > # The 10-second timer will never fire. > > pt = EventMachine::PeriodicTimer.new (1) {puts Time.new} > EventMachine::Timer.new(5) {pt.cancel} > # pt will fire once a second for five seconds. > > n = 6 > pt1 = EventMachine::PeriodicTimer.new(1) { > puts "n=#{n}" > n -= 1 > pt1.cancel if n == 0 #Timer cancels itself after firing six times. > } > } >EventMachine::Timer.new(5) {timer.cancel} I think in both cases, just a timer.cancel would be enough, where timer is the object created earlier!
Francis Cianfrocca
2007-May-04 06:38 UTC
[Eventmachine-talk] what would be simplest way to remove a
On 5/4/07, hemant <gethemant at gmail.com> wrote:> > I think in both cases, just a timer.cancel would be enough, where > timer is the object created earlier! > _Yes, but I wanted to give a complete working example. Obviously the #cancel calls can be done from receive_data handlers or anywhere else. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20070504/ff33c227/attachment.html