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

break on ctrl+c #183

Open
rriemann opened this issue Jul 25, 2016 · 3 comments
Open

break on ctrl+c #183

rriemann opened this issue Jul 25, 2016 · 3 comments

Comments

@rriemann
Copy link

Hello,

I use Parallel.map to run some computations in parallel. Is it possible to trap ctrl+c or ctrl+d and break after current tasks are done? So far, the script stops entirely and my intermediate results are not saved.

@grosser
Copy link
Owner

grosser commented Jul 25, 2016

you can do your own trapping and then raise a Parallel::Break
or check the presence of some file and then break ...

On Mon, Jul 25, 2016 at 2:09 PM, Robert Riemann [email protected]
wrote:

Hello,

I use Parallel.map to run some computations in parallel. Is it possible to
trap ctrl+c or ctrl+d and break after current tasks are done? So far, the
script stops entirely and my intermediate results are not saved.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#183, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAAsZwVo6AMz83RJ5JFukFwrHeSjlTnaks5qZSX5gaJpZM4JUjOR
.

@rriemann
Copy link
Author

rriemann commented Jul 26, 2016

I've tried something like this:

begin
  measurements = Parallel.map(measurements, progress: "Simulating") do |measurement|
    trap("SIGINT") { Parallel::Break }
    measurement[:cmd] = "java -jar app.jar --present-peers #{measurement[:presentPeers]} --deadline #{deadline} --byzantine-peers #{measurement[:byzantinePeers]} --seed #{measurement[:seed]} --log=jmsg.thres:error 2> /dev/null"
    measurement[:result] = `#{measurement[:cmd]}`.to_f
    measurement[:exitcode] = $?.to_i
    pp measurement
    measurement
  end
rescue Interrupt

end

However, when Parallel::Break is issued, the array measurements supposed to be fillled by Parallel.map is empty. :(

@grosser
Copy link
Owner

grosser commented Jul 27, 2016

oh yeah ... break makes it return nil ... as rubies break does ...

should be able to use the finish hook ...

require 'parallel'
done = []
finish = lambda { |_,_,x| done << x; puts "DONE #{done.inspect}"  }
begin
  Parallel.each(Array.new(10), finish: finish) { |i| sleep 1; 2 }
rescue Interrupt
end
puts "end #{done.inspect}"

ruby xxx.rb
DONE [2]
DONE [2, 2]
DONE [2, 2, 2]
DONE [2, 2, 2, 2]
^CParallel execution interrupted, exiting ...
end [2, 2, 2, 2]

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