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

use max Etc.nprocessors processes at a time #140

Merged
merged 2 commits into from
Jul 1, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 16 additions & 9 deletions lib/jekyll/polyglot/patches/jekyll/site.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'etc'

include Process
module Jekyll
class Site
Expand Down Expand Up @@ -31,22 +33,27 @@ def process
prepare
all_langs = (@languages + [@default_lang]).uniq
if @parallel_localization
nproc = Etc.nprocessors
pids = {}
all_langs.each do |lang|
pids[lang] = fork do
process_language lang
begin
all_langs.each do |lang|
pids[lang] = fork do
process_language lang
end
while pids.length >= (lang == all_langs[-1] ? 1 : nproc)
sleep 0.1
pids.map do |lang, pid|
pids.delete lang if waitpid pid, Process::WNOHANG
end
end
end
end
Signal.trap('INT') do
rescue Interrupt
all_langs.each do |lang|
next unless pids.key? lang
puts "Killing #{pids[lang]} : #{lang}"
kill('INT', pids[lang])
end
end
all_langs.each do |lang|
waitpid pids[lang]
detach pids[lang]
end
else
all_langs.each do |lang|
process_language lang
Expand Down
12 changes: 12 additions & 0 deletions spec/jekyll/polyglot/patches/jekyll/site_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,18 @@
end
end

describe @site do
it 'should spawn no more than Etc.nprocessors processes' do
forks = 0
allow(Etc).to receive(:nprocessors).and_return(2)
allow(@site).to receive(:fork) { forks += 1; fork { sleep 2 } }
thr = Thread.new { sleep 1; forks }
@site.process
expect(thr.value).to eq(Etc.nprocessors)
expect(forks).to eq((@langs + [@default_lang]).uniq.length)
end
end

# describe @relativize_urls do
# it 'does not raise' do
# expect(@relativize_urls(@site.Document.new(), @relative_url_regex)).not_to raise_error
Expand Down