concurrent_rails 0.1.2 → 0.1.3
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 +1 -0
- data/lib/concurrent_rails/multi.rb +1 -1
- data/lib/concurrent_rails/promises.rb +48 -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: afa4c6f568f01ea4ada14fabc78e2e47b7c9a8c3f7ddaa3f3cbc8d3c39dad949
|
4
|
+
data.tar.gz: cc4165e43a1a6a48fc0dd2bb5b6154257af0ab4d8c32a3620c61b6425e3f32c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f68e51f1d5fdc986e4fe70a16beac361f77334727da261ae545dc725f2d9424c9d9b0df7755e7b1e8fb22b010d03db253e1c5206217e13640b246c60c9330177
|
7
|
+
data.tar.gz: d20462ad8cd6a4b72c86e36654441b53b614e0c98684f4c087d5b7fff251e7dbe8f2e4c1627df623d2e332da8c6017b941d0d922160f78c27023ed942f3adf4f
|
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 { sleep(5); 42 }
|
21
|
+
=> #<ConcurrentRails::Promises:0x00007fe92ea9ff28 @future_instance=#<Con...
|
22
|
+
|
23
|
+
irb(main):003:0> future.state
|
24
|
+
=> :pending
|
25
|
+
|
26
|
+
# After future is processed
|
27
|
+
irb(main):004:0> future.state
|
28
|
+
=> :fulfilled
|
29
|
+
|
30
|
+
irb(main):005: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):006:0> future = ConcurrentRails::Promises.future { 42 }.then { |v| v * 2 }
|
38
|
+
=> #<ConcurrentRails::Promises:0x00007fe92eba3460 @future_instance=#...
|
39
|
+
irb(main):007: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.3'
|
118
147
|
```
|
119
148
|
|
120
149
|
And then execute:
|
data/lib/concurrent_rails.rb
CHANGED
@@ -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
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ConcurrentRails
|
4
|
+
class Promises
|
5
|
+
include Concurrent::Promises::FactoryMethods
|
6
|
+
extend Forwardable
|
7
|
+
|
8
|
+
def self.future(*args, &block)
|
9
|
+
new.run_on_rails(*args, &block)
|
10
|
+
end
|
11
|
+
|
12
|
+
def then(*args, &block)
|
13
|
+
@future_instance = Rails.application.executor.wrap do
|
14
|
+
future_instance.then(*args, &block)
|
15
|
+
end
|
16
|
+
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
%i[value value!].each do |method_name|
|
21
|
+
define_method method_name do
|
22
|
+
Rails.application.executor.wrap do
|
23
|
+
result = nil
|
24
|
+
|
25
|
+
ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
|
26
|
+
result = future_instance.__send__(method_name)
|
27
|
+
end
|
28
|
+
|
29
|
+
result
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def run_on_rails(*args, &block)
|
35
|
+
@future_instance = Rails.application.executor.wrap do
|
36
|
+
future(args, &block)
|
37
|
+
end
|
38
|
+
|
39
|
+
self
|
40
|
+
end
|
41
|
+
|
42
|
+
def_delegators :@future_instance, :state, :reason, :rejected?, :complete?
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
attr_reader :task, :future_instance
|
47
|
+
end
|
48
|
+
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.3
|
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-19 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/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
|
- - ">="
|