diff --git a/CHANGELOG.md b/CHANGELOG.md index 8efece7..4b8ea4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Added / Fixed / Changed -- Add here when making a PR +- `exit!: true` option to skip at_exit hooks and VM teardown in worker processes ## 2.2.0 diff --git a/Readme.md b/Readme.md index 59c8138..10ce22d 100644 --- a/Readme.md +++ b/Readme.md @@ -210,6 +210,7 @@ Tips - [Benchmark/Test] Disable threading/forking with `in_threads: 0` or `in_processes: 0`, to run the same code with different setups - [Isolation] Do not reuse previous worker processes: `isolation: true` + - [Fast worker shutdown] Workers skip at_exit hooks and VM teardown, which are slow in large processes: `exit!: true` - [Stop all processes with an alternate interrupt signal] `'INT'` (from `ctrl+c`) is caught by default. Catch `'TERM'` (from `kill`) with `interrupt_signal: 'TERM'` - [Process count via ENV] `PARALLEL_PROCESSOR_COUNT=16` will use `16` instead of the number of processors detected. This is used to reconfigure a tool using `parallel` without inserting custom logic. - [Process count] `parallel` uses a number of processors seen by the OS for process count by default. If you want to use a value considering CPU quota, please add `concurrent-ruby` to your `Gemfile`. diff --git a/lib/parallel.rb b/lib/parallel.rb index fd3bd93..3464eeb 100644 --- a/lib/parallel.rb +++ b/lib/parallel.rb @@ -684,8 +684,11 @@ def worker(job_factory, options, &block) process_incoming_jobs(child_read, child_write, job_factory, options, &block) ensure child_read.close - child_write.close + child_write.close # also flushes results buffered in child_write end + + # skip at_exit hooks and VM teardown, which are slow in large processes + exit!(0) if options[:exit!] end child_read.close diff --git a/spec/cases/map_exit_bang.rb b/spec/cases/map_exit_bang.rb new file mode 100644 index 0000000..a3d2d9d --- /dev/null +++ b/spec/cases/map_exit_bang.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true +require './spec/cases/helper' + +at_exit { puts "AT_EXIT #{Process.pid}" } + +result = Parallel.map([1, 2, 3, 4], in_processes: 2, exit!: true) { |i| i * 2 } +puts(result == [2, 4, 6, 8] ? "OK" : "FAIL: #{result.inspect}") diff --git a/spec/parallel_spec.rb b/spec/parallel_spec.rb index c93db37..799b26f 100644 --- a/spec/parallel_spec.rb +++ b/spec/parallel_spec.rb @@ -471,6 +471,11 @@ def cpus out.should == "0,1\nOK" end + it 'skips at_exit hooks in workers with exit!: true' do + out = ruby("spec/cases/map_exit_bang.rb") + out.should match(/\AOK\nAT_EXIT \d+\n\z/) # only the parent runs at_exit + end + it 'can use Timeout' do out = ruby("spec/cases/timeout_in_threads.rb") out.should == "OK\n"