Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
5 changes: 4 additions & 1 deletion lib/parallel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/cases/map_exit_bang.rb
Original file line number Diff line number Diff line change
@@ -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}")
5 changes: 5 additions & 0 deletions spec/parallel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading