Skip to content

Commit 6dd951f

Browse files
committed
add parallelism to Crystal
1 parent f6dd7ad commit 6dd951f

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

words_extractor_cr/src/fast_words_cr.cr

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,29 @@ module FastWordsCr
99

1010
def self.main(outpath = "words")
1111
with_sorting = true
12+
concurrent = true
13+
1214
prepare_folder(outpath, "*.txt")
15+
16+
file_count = 0
17+
18+
processed_files = Channel(Bool).new
1319
Dir.glob("../data/pl/**/*.yml").each do |path|
14-
# spawn do
20+
if concurrent
21+
spawn do
22+
worker(path, outpath, with_sorting)
23+
processed_files.send true
24+
end
25+
file_count += 1
26+
else
1527
worker(path, outpath, with_sorting)
16-
# end
28+
end
29+
end
30+
if concurrent
31+
file_count.times do
32+
processed_files.receive
33+
end
1734
end
18-
# Fiber.yield
1935
end
2036

2137
def self.worker(path, outpath, with_sorting)
@@ -24,7 +40,7 @@ module FastWordsCr
2440
words = text.split(/[^\p{L}]+/).to_set
2541

2642
if with_sorting
27-
words = words.to_a.sort {|x, y| self.word_cmp(x, y)}
43+
words = words.to_a.sort { |x, y| self.word_cmp(x, y) }
2844
end
2945

3046
meta = File.open(path) { |file| YAML.parse(file) }

0 commit comments

Comments
 (0)