concurrent_rails 0.1.1 → 0.1.6
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 +50 -0
- data/lib/concurrent_rails/version.rb +1 -1
- metadata +17 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83f0c6f9839dd61c1148dcb02f2a0c4b12d61294af1f7f4f86487f0f0d5f3907
|
4
|
+
data.tar.gz: 2a2a8eddbad74a6a56a6ad7f3b28b46c7c8c1ba7a0c44a30c0a3d0b25496072d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8bb76a46611d36597321ec8b4fd700a86a844f1f489791005e845c2947893c83652d2675d359ccc628fdc810606ade432e58c1406aa410412e1f80f444a33509
|
7
|
+
data.tar.gz: a5e3a549eb26a86fb9887c3d86ce4aeea71bd604ac8fb42a204b56063d34b7c914f3518bd189fd38a9336f36fc762049905919812eb12d1373cb24139adba76c
|
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` 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.6'
|
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,50 @@
|
|
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
|
+
%i[value value!].each do |method_name|
|
27
|
+
define_method method_name do |timeout = nil, timeout_value = nil|
|
28
|
+
Rails.application.executor.wrap do
|
29
|
+
ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
|
30
|
+
future_instance.__send__(method_name, timeout, timeout_value)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def with_rails(executor, *args, &task)
|
37
|
+
@future_instance = Rails.application.executor.wrap do
|
38
|
+
future_on(executor, *args, &task)
|
39
|
+
end
|
40
|
+
|
41
|
+
self
|
42
|
+
end
|
43
|
+
|
44
|
+
def_delegators :@future_instance, :state, :reason, :rejected?, :complete?
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
attr_reader :future_instance
|
49
|
+
end
|
50
|
+
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.6
|
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-04-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,40 +16,40 @@ 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
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '1.12'
|
34
|
-
type: :
|
34
|
+
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.12'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rubocop-performance
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '1.10'
|
48
|
-
type: :
|
48
|
+
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '1.10'
|
55
55
|
description: Small library to make concurrent-ruby and Rails play nice together
|
@@ -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
|
- - ">="
|