Skip to content

Commit eee25da

Browse files
NikitaNikita
authored andcommitted
task two parallel
1 parent 127401b commit eee25da

3 files changed

Lines changed: 38 additions & 12 deletions

File tree

day_2/tn_rails_concurrent/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ group :development do
2323
end
2424

2525
gem "parallel", "~> 1.26.3"
26+
gem "async"
2627
gem "sidekiq", "~> 7.3.6"
2728

2829
gem "activerecord-import", "~> 2.0"

day_2/tn_rails_concurrent/Gemfile.lock

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ GEM
8383
annotate (3.2.0)
8484
activerecord (>= 3.2, < 8.0)
8585
rake (>= 10.4, < 14.0)
86+
async (2.21.1)
87+
console (~> 1.29)
88+
fiber-annotation
89+
io-event (~> 1.6, >= 1.6.5)
8690
base64 (0.2.0)
8791
benchmark (0.4.0)
8892
bigdecimal (3.1.8)
@@ -92,13 +96,21 @@ GEM
9296
builder (3.3.0)
9397
concurrent-ruby (1.3.4)
9498
connection_pool (2.4.1)
99+
console (1.29.2)
100+
fiber-annotation
101+
fiber-local (~> 1.1)
102+
json
95103
crass (1.0.6)
96104
date (3.4.1)
97105
debug (1.9.2)
98106
irb (~> 1.10)
99107
reline (>= 0.3.8)
100108
drb (2.2.1)
101109
erubi (1.13.0)
110+
fiber-annotation (0.2.0)
111+
fiber-local (1.1.0)
112+
fiber-storage
113+
fiber-storage (1.0.0)
102114
globalid (1.2.1)
103115
activesupport (>= 6.1)
104116
i18n (1.14.6)
@@ -108,9 +120,11 @@ GEM
108120
activesupport (>= 6.0.0)
109121
railties (>= 6.0.0)
110122
io-console (0.8.0)
123+
io-event (1.7.5)
111124
irb (1.14.2)
112125
rdoc (>= 4.0.0)
113126
reline (>= 0.4.2)
127+
json (2.9.1)
114128
logger (1.6.3)
115129
loofah (2.23.1)
116130
crass (~> 1.0.2)
@@ -235,6 +249,7 @@ PLATFORMS
235249
DEPENDENCIES
236250
activerecord-import (~> 2.0)
237251
annotate
252+
async
238253
bootsnap
239254
debug
240255
importmap-rails

day_2/tn_rails_concurrent/lib/tasks/task_to_be_optimized.rake

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,37 @@ namespace :orders do
99

1010
Order.destroy_all
1111
# Оптимизировать тут
12-
Array.new(200) { Order.create!(product_id: rand(1..100), quantity: rand(1..10), current_status: :pending) }
12+
Parallel.map(Array.new(200)) do
13+
{ product_id: rand(1..100), quantity: rand(1..10), current_status: :pending }
14+
end.tap { Order.insert_all(_1) }
1315

1416
time = Benchmark.realtime do
1517
puts Order.pending.count
1618
# Оптимизировать тут
17-
Order.pending.find_in_batches(batch_size: 50) do |batch|
18-
process_batch_with_http(batch)
19+
Async do
20+
Order.pending.find_in_batches(batch_size: 50) do |batch|
21+
Async { process_batch_with_http(batch) }
22+
end
1923
end
2024
end
2125

2226
puts "Processed orders in #{time.round(2)} seconds with HTTP request"
2327
end
2428

2529
def process_batch_with_http(batch)
26-
batch.each do |order|
30+
batch.map do |order|
2731
# Оптимизировать тут
28-
response = send_to_external_service
29-
if response.code.to_i == 200
30-
order.process!
31-
puts "Successfully processed Order ##{order.id}"
32-
else
33-
raise "Failed to process Order ##{order.id}: #{response.body}"
32+
Async do
33+
response = send_to_external_service
34+
if response.code.to_i == 200
35+
order.process!
36+
puts "Successfully processed Order ##{order.id}"
37+
else
38+
raise "Failed to process Order ##{order.id}: #{response.body}"
39+
end
40+
rescue StandardError => e
41+
Rails.logger.error("Error processing Order ##{order.id}: #{e.message}")
3442
end
35-
rescue StandardError => e
36-
Rails.logger.error("Error processing Order ##{order.id}: #{e.message}")
3743
end
3844
end
3945

@@ -47,3 +53,7 @@ namespace :orders do
4753
http.request(request)
4854
end
4955
end
56+
57+
# Processed orders in 20.1 seconds with HTTP request (перед оптимизацией)
58+
# Processed orders in 0.11 seconds with HTTP request (async при io bound, parallel при cpu)
59+
# Processed orders in 0.2 seconds with HTTP request (thread при io bound, parallel при cpu)

0 commit comments

Comments
 (0)