concurrent_rails 0.1.2 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +32 -3
- data/lib/concurrent_rails.rb +3 -2
- data/lib/concurrent_rails/future.rb +8 -11
- data/lib/concurrent_rails/multi.rb +5 -1
- data/lib/concurrent_rails/promises.rb +58 -0
- data/lib/concurrent_rails/version.rb +1 -1
- metadata +11 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe2277deed1f15f3c47a332667db0b85bbfb0d39d58bcb6539fa0d15d843c55d
|
4
|
+
data.tar.gz: 0e347f69da65b60fcbdb63a04cdcb3bac782ba01b6e543db93428ff4c2bdbdc0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '079c165494e1eea8ae9c98fe89031c87408808527a3f90eda3c5c5d87598bfb5932b0285acaa99d4e5e714a1012af3676bc16fc9b5929268ff26640b90f4b191'
|
7
|
+
data.tar.gz: 97944d3f3db134d08eb894f6f3211a705cb080ec4d4ec331657c94940fd08a08c4f1f46859d430f3c2e5097a1cc54d7de322e9f39a0fcc48de78d49982cd26e8
|
data/README.md
CHANGED
@@ -9,11 +9,40 @@ 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
|
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
|
+
|
14
|
+
### Promises
|
15
|
+
|
16
|
+
`Promises` is the recommended way from `concurrent-ruby` to create `Future`s as `Concurrent::Future` will be deprecated at some point.
|
17
|
+
Similar to other classes, all you have to do is call `.future` helper and pass a block:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
irb(main):001:0> future = ConcurrentRails::Promises.future(5) { |v| sleep(v); 42 }
|
21
|
+
=> #<ConcurrentRails::Promises:0x00007fed68db66b0 @future_instance=#<Concurrent::Promises::Future
|
22
|
+
|
23
|
+
irb(main):002:0> future.state
|
24
|
+
=> :pending
|
25
|
+
|
26
|
+
# After the process slept for 5 seconds
|
27
|
+
irb(main):003:0> future.state
|
28
|
+
=> :fulfilled
|
29
|
+
|
30
|
+
irb(main):004:0> future.value
|
31
|
+
=> 42
|
32
|
+
```
|
33
|
+
|
34
|
+
The benefit of `Promises` over a pure `Future` class is that you can chain futures without blocking the main thread.
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
irb(main):001:0> future = ConcurrentRails::Promises.future { 42 }.then { |v| v * 2 }
|
38
|
+
=> #<ConcurrentRails::Promises:0x00007fe92eba3460 @future_instance=#...
|
39
|
+
irb(main):002:0> future.value
|
40
|
+
=> 84
|
41
|
+
```
|
13
42
|
|
14
43
|
### Future
|
15
44
|
|
16
|
-
`ConcurrentRails::Future` will execute your code in a separated thread and you can check the progress of it whenever you need. When the task is ready, you can access the result with `#result` function:
|
45
|
+
`ConcurrentRails::Future` will execute your code in a separated thread and you can check the progress of it whenever you need it. When the task is ready, you can access the result with `#result` function:
|
17
46
|
|
18
47
|
```ruby
|
19
48
|
irb(main):001:0> future = ConcurrentRails::Future.new do
|
@@ -114,7 +143,7 @@ For more information on how Futures work and how Rails handle multithread check
|
|
114
143
|
Add this line to your application's Gemfile:
|
115
144
|
|
116
145
|
```ruby
|
117
|
-
gem 'concurrent_rails'
|
146
|
+
gem 'concurrent_rails', '~> 0.1.7'
|
118
147
|
```
|
119
148
|
|
120
149
|
And then execute:
|
data/lib/concurrent_rails.rb
CHANGED
@@ -1,9 +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'
|
5
|
+
require 'concurrent_rails/promises'
|
6
|
+
require 'concurrent_rails/railtie'
|
7
|
+
require 'concurrent_rails/version'
|
7
8
|
|
8
9
|
module ConcurrentRails
|
9
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.application.executor.wrap 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
|
|
@@ -40,6 +37,6 @@ module ConcurrentRails
|
|
40
37
|
end
|
41
38
|
end
|
42
39
|
|
43
|
-
attr_reader :executor, :
|
40
|
+
attr_reader :executor, :future
|
44
41
|
end
|
45
42
|
end
|
@@ -4,7 +4,7 @@ module ConcurrentRails
|
|
4
4
|
class Multi
|
5
5
|
def self.enqueue(*actions, executor: :io)
|
6
6
|
unless actions.all? { |action| action.is_a?(Proc) }
|
7
|
-
raise ArgumentError, '#enqueue accepts `Proc`s only'
|
7
|
+
raise ArgumentError, '#enqueue accepts `Proc`s only'
|
8
8
|
end
|
9
9
|
|
10
10
|
new(actions, executor).enqueue
|
@@ -30,6 +30,10 @@ module ConcurrentRails
|
|
30
30
|
futures.map(&:value)
|
31
31
|
end
|
32
32
|
|
33
|
+
def compute!
|
34
|
+
futures.map(&:value!)
|
35
|
+
end
|
36
|
+
|
33
37
|
def complete?
|
34
38
|
futures.all?(&:complete?)
|
35
39
|
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ConcurrentRails
|
4
|
+
class Promises
|
5
|
+
extend Forwardable
|
6
|
+
include Concurrent::Promises::FactoryMethods
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def future(*args, &task)
|
10
|
+
future_on(:fast, *args, &task)
|
11
|
+
end
|
12
|
+
|
13
|
+
def future_on(executor, *args, &task)
|
14
|
+
new.with_rails(executor, *args, &task)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def then(*args, &task)
|
19
|
+
@future_instance = Rails.application.executor.wrap do
|
20
|
+
future_instance.then(*args, &task)
|
21
|
+
end
|
22
|
+
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def chain(*args, &task)
|
27
|
+
Rails.application.executor.wrap do
|
28
|
+
future_instance.chain(*args, &task)
|
29
|
+
end
|
30
|
+
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
%i[value value!].each do |method_name|
|
35
|
+
define_method method_name do |timeout = nil, timeout_value = nil|
|
36
|
+
Rails.application.executor.wrap do
|
37
|
+
ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
|
38
|
+
future_instance.__send__(method_name, timeout, timeout_value)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def with_rails(executor, *args, &task)
|
45
|
+
@future_instance = Rails.application.executor.wrap do
|
46
|
+
future_on(executor, *args, &task)
|
47
|
+
end
|
48
|
+
|
49
|
+
self
|
50
|
+
end
|
51
|
+
|
52
|
+
def_delegators :@future_instance, :state, :reason, :rejected?, :resolved?
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
attr_reader :future_instance
|
57
|
+
end
|
58
|
+
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.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- Luiz Eduardo
|
7
|
+
- Luiz Eduardo Kowalski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-04
|
11
|
+
date: 2021-05-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '5.
|
19
|
+
version: '5.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '5.
|
26
|
+
version: '5.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rubocop
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -65,16 +65,17 @@ files:
|
|
65
65
|
- lib/concurrent_rails.rb
|
66
66
|
- lib/concurrent_rails/future.rb
|
67
67
|
- lib/concurrent_rails/multi.rb
|
68
|
+
- lib/concurrent_rails/promises.rb
|
68
69
|
- lib/concurrent_rails/railtie.rb
|
69
70
|
- lib/concurrent_rails/version.rb
|
70
71
|
- lib/tasks/concurrent_rails_tasks.rake
|
71
|
-
homepage: https://github.com/luizkowalski/
|
72
|
+
homepage: https://github.com/luizkowalski/concurrent_rails
|
72
73
|
licenses:
|
73
74
|
- MIT
|
74
75
|
metadata:
|
75
|
-
homepage_uri: https://github.com/luizkowalski/
|
76
|
-
source_code_uri: https://github.com/luizkowalski/
|
77
|
-
changelog_uri: https://github.com/luizkowalski/
|
76
|
+
homepage_uri: https://github.com/luizkowalski/concurrent_rails
|
77
|
+
source_code_uri: https://github.com/luizkowalski/concurrent_rails
|
78
|
+
changelog_uri: https://github.com/luizkowalski/concurrent_rails/blob/master/CHANGELOG.md
|
78
79
|
post_install_message:
|
79
80
|
rdoc_options: []
|
80
81
|
require_paths:
|
@@ -83,7 +84,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
83
84
|
requirements:
|
84
85
|
- - ">="
|
85
86
|
- !ruby/object:Gem::Version
|
86
|
-
version: '2.
|
87
|
+
version: '2.6'
|
87
88
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
89
|
requirements:
|
89
90
|
- - ">="
|