Skip to content

Instantly share code, notes, and snippets.

@yankov
Created July 1, 2012 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yankov/3029115 to your computer and use it in GitHub Desktop.
Save yankov/3029115 to your computer and use it in GitHub Desktop.
Stop EM when all iterations are complete
# is there a good pattern for this routine?
EM.synchrony do
size = keys.count
counter = 0
# check if each iteration was completed
EM::add_periodic_timer( 2 ) do
next unless counter == size
EM.stop
end
EM::Synchrony::FiberIterator.new(keys, 2000).each {|key|
do_something_with(key).callback { counter += 1; }
}
end
@khakimov
Copy link

khakimov commented Jul 2, 2012

# why checking each 2 sec? 

EM.synchrony do

  size = keys.count
  counter = 0

  EM::Synchrony::FiberIterator.new(keys, 2000).each {|key|
    # .callback -> counter = +1 -> if counter == size -> EM.stop
    EM.stop if counter == size
    do_something_with(key).callback { counter += 1; }
  }

end

@yankov
Copy link
Author

yankov commented Jul 2, 2012

yeah, I tried this pattern at another place too: https://github.com/yankov/redis-migrator/blob/master/migrator.rb#L43.
But the point was that it can be more complicated than just comparing a counter, and counter could be changed from various other places.

@yankov
Copy link
Author

yankov commented Jul 2, 2012

Anyways, EM turned out to be not the best approach for this job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment