concurrent_rails 0.1.3 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -10
- data/lib/concurrent_rails.rb +2 -2
- data/lib/concurrent_rails/future.rb +14 -13
- data/lib/concurrent_rails/multi.rb +4 -0
- data/lib/concurrent_rails/promises.rb +36 -23
- data/lib/concurrent_rails/version.rb +1 -1
- metadata +3 -4
- data/lib/tasks/concurrent_rails_tasks.rake +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 257bcdcbd99076d7b73c193297ce5bc410c1fd5962bb823b965b5abbdf21197c
|
4
|
+
data.tar.gz: 5ae2230ba4416193fac70bcd9500673e7d96081491786717eb8d6ce95c938f92
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df4405c6f8e8efe155dea24db8a3799955b59c4d3a93118153b557711941c66428027caea64d245df4437be49d3d0482ffb2461bad863b38877e152f57eb6970
|
7
|
+
data.tar.gz: 8c11ffcd0afbeedf533a8da19ca19f79a1e2566e326946f55e699337f45c179a2f245f5b4fd46f62b694a9803ac317cf0bfb6dec95c38285cf3487c5631a4457
|
data/README.md
CHANGED
@@ -9,7 +9,7 @@ The goal of this gem is to provide a simple library that allows the developer to
|
|
9
9
|
|
10
10
|
## Usage
|
11
11
|
|
12
|
-
This library provides three classes that will help you run tasks in parallel: `ConcurrentRails::Promises`, `ConcurrentRails::Future` and `ConcurrentRails::Multi`
|
12
|
+
This library provides three classes that will help you run tasks in parallel: `ConcurrentRails::Promises`, `ConcurrentRails::Future` ([in process of being deprecated by concurrent-ruby](https://github.com/ruby-concurrency/concurrent-ruby#deprecated)) and `ConcurrentRails::Multi`
|
13
13
|
|
14
14
|
### Promises
|
15
15
|
|
@@ -17,26 +17,26 @@ This library provides three classes that will help you run tasks in parallel: `C
|
|
17
17
|
Similar to other classes, all you have to do is call `.future` helper and pass a block:
|
18
18
|
|
19
19
|
```ruby
|
20
|
-
irb(main):001:0> future = ConcurrentRails::Promises.future { sleep(
|
21
|
-
=> #<ConcurrentRails::Promises:
|
20
|
+
irb(main):001:0> future = ConcurrentRails::Promises.future(5) { |v| sleep(v); 42 }
|
21
|
+
=> #<ConcurrentRails::Promises:0x00007fed68db66b0 @future_instance=#<Concurrent::Promises::Future
|
22
22
|
|
23
|
-
irb(main):
|
23
|
+
irb(main):002:0> future.state
|
24
24
|
=> :pending
|
25
25
|
|
26
|
-
# After
|
27
|
-
irb(main):
|
26
|
+
# After the process slept for 5 seconds
|
27
|
+
irb(main):003:0> future.state
|
28
28
|
=> :fulfilled
|
29
29
|
|
30
|
-
irb(main):
|
30
|
+
irb(main):004:0> future.value
|
31
31
|
=> 42
|
32
32
|
```
|
33
33
|
|
34
34
|
The benefit of `Promises` over a pure `Future` class is that you can chain futures without blocking the main thread.
|
35
35
|
|
36
36
|
```ruby
|
37
|
-
irb(main):
|
37
|
+
irb(main):001:0> future = ConcurrentRails::Promises.future { 42 }.then { |v| v * 2 }
|
38
38
|
=> #<ConcurrentRails::Promises:0x00007fe92eba3460 @future_instance=#...
|
39
|
-
irb(main):
|
39
|
+
irb(main):002:0> future.value
|
40
40
|
=> 84
|
41
41
|
```
|
42
42
|
|
@@ -143,7 +143,7 @@ For more information on how Futures work and how Rails handle multithread check
|
|
143
143
|
Add this line to your application's Gemfile:
|
144
144
|
|
145
145
|
```ruby
|
146
|
-
gem 'concurrent_rails', '~> 0.1.
|
146
|
+
gem 'concurrent_rails', '~> 0.1.8'
|
147
147
|
```
|
148
148
|
|
149
149
|
And then execute:
|
data/lib/concurrent_rails.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'concurrent_rails/version'
|
4
|
-
require 'concurrent_rails/railtie'
|
5
3
|
require 'concurrent_rails/future'
|
6
4
|
require 'concurrent_rails/multi'
|
7
5
|
require 'concurrent_rails/promises'
|
6
|
+
require 'concurrent_rails/railtie'
|
7
|
+
require 'concurrent_rails/version'
|
8
8
|
|
9
9
|
module ConcurrentRails
|
10
10
|
end
|
@@ -4,8 +4,7 @@ module ConcurrentRails
|
|
4
4
|
class Future
|
5
5
|
extend Forwardable
|
6
6
|
|
7
|
-
def initialize(executor: :
|
8
|
-
@task = block
|
7
|
+
def initialize(executor: :fast, &block)
|
9
8
|
@executor = executor
|
10
9
|
@future = run_on_rails(block)
|
11
10
|
end
|
@@ -16,15 +15,13 @@ module ConcurrentRails
|
|
16
15
|
self
|
17
16
|
end
|
18
17
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
18
|
+
%i[value value!].each do |method_name|
|
19
|
+
define_method method_name do
|
20
|
+
rails_wrapped do
|
21
|
+
ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
|
22
|
+
future.__send__(method_name)
|
23
|
+
end
|
25
24
|
end
|
26
|
-
|
27
|
-
result
|
28
25
|
end
|
29
26
|
end
|
30
27
|
|
@@ -33,13 +30,17 @@ module ConcurrentRails
|
|
33
30
|
private
|
34
31
|
|
35
32
|
def run_on_rails(block)
|
36
|
-
@future =
|
33
|
+
@future = rails_wrapped do
|
37
34
|
Concurrent::Future.new(executor: executor) do
|
38
|
-
|
35
|
+
rails_wrapped(&block)
|
39
36
|
end
|
40
37
|
end
|
41
38
|
end
|
42
39
|
|
43
|
-
|
40
|
+
def rails_wrapped(&block)
|
41
|
+
Rails.application.executor.wrap(&block)
|
42
|
+
end
|
43
|
+
|
44
|
+
attr_reader :executor, :future
|
44
45
|
end
|
45
46
|
end
|
@@ -2,47 +2,60 @@
|
|
2
2
|
|
3
3
|
module ConcurrentRails
|
4
4
|
class Promises
|
5
|
-
include Concurrent::Promises::FactoryMethods
|
6
5
|
extend Forwardable
|
6
|
+
include Concurrent::Promises::FactoryMethods
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
class << self
|
9
|
+
def future(*args, &task)
|
10
|
+
future_on(:fast, *args, &task)
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
future_instance.then(*args, &block)
|
13
|
+
def future_on(executor, *args, &task)
|
14
|
+
new(executor).run_on_rails(*args, &task)
|
15
15
|
end
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
+
def initialize(executor)
|
19
|
+
@executor = executor
|
18
20
|
end
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
Rails.application.executor.wrap do
|
23
|
-
result = nil
|
22
|
+
def run_on_rails(*args, &task)
|
23
|
+
@future_instance = rails_wrapped { future_on(executor, *args, &task) }
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
end
|
25
|
+
self
|
26
|
+
end
|
28
27
|
|
29
|
-
|
28
|
+
%i[then chain].each do |chainable|
|
29
|
+
define_method(chainable) do |*args, &task|
|
30
|
+
method = "#{chainable}_on"
|
31
|
+
@future_instance = rails_wrapped do
|
32
|
+
future_instance.__send__(method, executor, *args, &task)
|
30
33
|
end
|
34
|
+
|
35
|
+
self
|
31
36
|
end
|
32
37
|
end
|
33
38
|
|
34
|
-
|
35
|
-
|
36
|
-
|
39
|
+
%i[value value!].each do |method_name|
|
40
|
+
define_method(method_name) do |timeout = nil, timeout_value = nil|
|
41
|
+
rails_wrapped do
|
42
|
+
ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
|
43
|
+
future_instance.__send__(method_name, timeout, timeout_value)
|
44
|
+
end
|
45
|
+
end
|
37
46
|
end
|
38
|
-
|
39
|
-
self
|
40
47
|
end
|
41
48
|
|
42
|
-
|
49
|
+
%i[state reason rejected? resolved? fulfilled?].each do |delegatable|
|
50
|
+
def_delegator :@future_instance, delegatable
|
51
|
+
end
|
43
52
|
|
44
53
|
private
|
45
54
|
|
46
|
-
|
55
|
+
def rails_wrapped(&block)
|
56
|
+
Rails.application.executor.wrap(&block)
|
57
|
+
end
|
58
|
+
|
59
|
+
attr_reader :future_instance, :executor
|
47
60
|
end
|
48
61
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: concurrent_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Luiz Eduardo Kowalski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-05-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -68,14 +68,13 @@ files:
|
|
68
68
|
- lib/concurrent_rails/promises.rb
|
69
69
|
- lib/concurrent_rails/railtie.rb
|
70
70
|
- lib/concurrent_rails/version.rb
|
71
|
-
- lib/tasks/concurrent_rails_tasks.rake
|
72
71
|
homepage: https://github.com/luizkowalski/concurrent_rails
|
73
72
|
licenses:
|
74
73
|
- MIT
|
75
74
|
metadata:
|
76
75
|
homepage_uri: https://github.com/luizkowalski/concurrent_rails
|
77
76
|
source_code_uri: https://github.com/luizkowalski/concurrent_rails
|
78
|
-
changelog_uri: https://github.com/luizkowalski/concurrent_rails/CHANGELOG.md
|
77
|
+
changelog_uri: https://github.com/luizkowalski/concurrent_rails/blob/master/CHANGELOG.md
|
79
78
|
post_install_message:
|
80
79
|
rdoc_options: []
|
81
80
|
require_paths:
|