concurrent_rails 0.6.1 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -99
- data/lib/concurrent_rails/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a745bd40255c338ca8e305b8d6477d91f6a249db984e613042df56f42af674b0
|
4
|
+
data.tar.gz: 7e141fc895855f868f0b1d34537ab27155c75ab8db3b833a237ca86ac889448e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8467ca3bd91d7393e163c85ac820d182a29df3cc5d8f52baf71fccb563c0a009fd54d125f5fa98598b9c051565479381e09b88e823f20f5c5dff8b909526e62d
|
7
|
+
data.tar.gz: 95b7ca4107bd52b51a769adb93b56ed1c54bf1bee8d797fc8db71524afec128b092a85505c2a0953c1458a21b551d0434ccfba591797385a148ed44c3cde33cd
|
data/README.md
CHANGED
@@ -40,7 +40,7 @@ irb(main):002:0> future.value
|
|
40
40
|
|
41
41
|
### Delayed futures
|
42
42
|
|
43
|
-
|
43
|
+
A delayed future is a Future that is enqueued but not run until `#touch` or any other method that requires a resolution is called.
|
44
44
|
|
45
45
|
```ruby
|
46
46
|
irb(main):002:0> delay = ConcurrentRails::Promises.delay { 42 }
|
@@ -65,9 +65,9 @@ Three methods will trigger a resolution: `#touch`, `#value` and `#wait`: `#touch
|
|
65
65
|
|
66
66
|
Delayed and regular futures can set a callback to be executed after the resolution of the future. There are three different callbacks:
|
67
67
|
|
68
|
-
* `on_resolution`: runs after the
|
68
|
+
* `on_resolution`: runs after the Future is resolved and yields three parameters to the callback in the following order: `true/false` for future's fulfillment, `value` as the result of the future execution, and `reason`, that will be `nil` if the future fulfilled or the error that the future triggered.
|
69
69
|
|
70
|
-
* `on_fulfillment`: runs after the
|
70
|
+
* `on_fulfillment`: runs after the Future is fulfilled and yields `value` to the callback
|
71
71
|
|
72
72
|
* `on_rejection`: runs after the future is rejected and yields the `error` to the callback
|
73
73
|
|
@@ -81,101 +81,9 @@ delay.touch
|
|
81
81
|
|
82
82
|
All of these callbacks have a bang version (e.g. `on_fulfillment!`). The bang version will execute the callback on the same thread pool that was initially set up and the version without bang will run asynchronously on a different executor.
|
83
83
|
|
84
|
-
### (Deprecated) Future
|
85
|
-
|
86
|
-
`ConcurrentRails::Future` will execute your code in a separate 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:
|
87
|
-
|
88
|
-
```ruby
|
89
|
-
irb(main):001:0> future = ConcurrentRails::Future.new do
|
90
|
-
sleep(5) # Simulate a long running task
|
91
|
-
42
|
92
|
-
end
|
93
|
-
|
94
|
-
# at this point, nothing has happened yet.
|
95
|
-
|
96
|
-
irb(main):002:0> future.execute
|
97
|
-
|
98
|
-
irb(main):003:0> future.state
|
99
|
-
=> :processing
|
100
|
-
|
101
|
-
# after 5 seconds
|
102
|
-
irb(main):004:0> future.state
|
103
|
-
=> :fulfilled
|
104
|
-
|
105
|
-
irb(main):005:0> future.value
|
106
|
-
=> 42
|
107
|
-
```
|
108
|
-
|
109
|
-
A task can also fail. In this case, the state of the future will be `rejected` and the exception can be accessed by invoking `reason`
|
110
|
-
|
111
|
-
```ruby
|
112
|
-
irb(main):001:1* future = ConcurrentRails::Future.new do
|
113
|
-
irb(main):002:1* 2 / 0
|
114
|
-
irb(main):003:0> end.execute
|
115
|
-
|
116
|
-
=> #<ConcurrentRails::Future...
|
117
|
-
|
118
|
-
irb(main):004:0> future.state
|
119
|
-
=> :rejected
|
120
|
-
|
121
|
-
irb(main):005:0> future.reason
|
122
|
-
=> #<ZeroDivisionError: divided by 0>
|
123
|
-
```
|
124
|
-
|
125
|
-
### (Deprecated) Multi
|
126
|
-
|
127
|
-
`ConcurrentRails::Multi` will let you execute multiple tasks in parallel and aggregate the results of each task when they are done. `Multi` accepts an undefined number of `Proc`s.
|
128
|
-
|
129
|
-
```ruby
|
130
|
-
irb(main):001:1* multi = ConcurrentRails::Multi.enqueue(
|
131
|
-
irb(main):002:1* -> { 42 },
|
132
|
-
irb(main):003:1* -> { :multi_test }
|
133
|
-
irb(main):004:0> )
|
134
|
-
|
135
|
-
=> #<ConcurrentRails::Multi:0x00007fbc3f9ca3f8 @actions=[#<Proc:0x00007fbc3f9ca470..
|
136
|
-
irb(main):005:0> multi.complete?
|
137
|
-
=> true
|
138
|
-
|
139
|
-
irb(main):006:0> multi.compute
|
140
|
-
=> [42, :multi_test]
|
141
|
-
```
|
142
|
-
|
143
|
-
Given the fact that you can send any number of `Proc`s, the result from `compute` will always be an array, even if you provide only one proc.
|
144
|
-
|
145
|
-
```ruby
|
146
|
-
irb(main):007:1* multi = ConcurrentRails::Multi.enqueue(
|
147
|
-
irb(main):008:1* -> { 42 }
|
148
|
-
irb(main):009:0> )
|
149
|
-
=> #<ConcurrentRails::Multi:0x00007fbc403f0b98 @actions=[#<Proc:0x00007...
|
150
|
-
|
151
|
-
irb(main):010:0> multi.compute
|
152
|
-
=> [42]
|
153
|
-
```
|
154
|
-
|
155
|
-
Same as `Future`, one of the `Multi` tasks can fail. You can access the exception by calling `#errors`:
|
156
|
-
|
157
|
-
```ruby
|
158
|
-
irb(main):001:1* multi = ConcurrentRails::Multi.enqueue(
|
159
|
-
irb(main):002:1* -> { 42 },
|
160
|
-
irb(main):003:1* -> { 2 / 0 }
|
161
|
-
irb(main):004:0> )
|
162
|
-
=> #<ConcurrentRails::Multi:0x00007fb46d3ee3a0 @actions=[#<Proc:0x00007..
|
163
|
-
|
164
|
-
irb(main):005:0> multi.complete?
|
165
|
-
=> true
|
166
|
-
|
167
|
-
irb(main):006:0> multi.compute
|
168
|
-
=> [42, nil]
|
169
|
-
|
170
|
-
irb(main):007:0> multi.errors
|
171
|
-
=> [#<ZeroDivisionError: divided by 0>]
|
172
|
-
```
|
173
|
-
|
174
|
-
It is worth mention that a failed proc will return `nil`.
|
175
|
-
|
176
84
|
## Testing
|
177
85
|
|
178
|
-
If you are using RSpec, you will notice that it might not play well with threads. ActiveRecord opens a database connection for every thread and since RSpec tests are wrapped in a transaction, by the time your promise tries to access something on the database, for example, a user, gems like Database Cleaner probably already triggered and deleted the user, resulting in `ActiveRecord::RecordNotFound` errors. You have a couple of solutions like
|
86
|
+
If you are using RSpec, you will notice that it might not play well with threads. ActiveRecord opens a database connection for every thread and since RSpec tests are wrapped in a transaction, by the time your promise tries to access something on the database, for example, a user, gems like Database Cleaner probably already triggered and deleted the user, resulting in `ActiveRecord::RecordNotFound` errors. You have a couple of solutions like disabling transactional fixtures if you are using it or update the Database Cleaner strategy (that will result in much slower tests).
|
179
87
|
Since none of these solutions were satisfactory to me, I created `ConcurrentRails::Testing` with two strategies: `immediate` and `fake`. When you wrap a Promise's `future` with `immediate`, the executor gets replaced from `:io` to `:immediate`. It still returns a promise anyway. This is not the case with `fake` strategy: it executes the task outside the `ConcurrentRails` engine and returns whatever `.value` would return:
|
180
88
|
|
181
89
|
`immediate` strategy:
|
@@ -204,11 +112,11 @@ irb(main):004:0> result.class
|
|
204
112
|
=> Integer
|
205
113
|
```
|
206
114
|
|
207
|
-
You can also set the
|
115
|
+
You can also set the strategy globally using `ConcurrentRails::Testing.fake!` or `ConcurrentRails::Testing.immediate!`
|
208
116
|
|
209
117
|
## Further reading
|
210
118
|
|
211
|
-
For more information on how Futures
|
119
|
+
For more information on how Futures works and how Rails handles multithread check these links:
|
212
120
|
|
213
121
|
[Future documentation](https://github.com/ruby-concurrency/concurrent-ruby/blob/master/docs-source/future.md)
|
214
122
|
|
@@ -236,7 +144,7 @@ gem install concurrent_rails
|
|
236
144
|
|
237
145
|
## Contributing
|
238
146
|
|
239
|
-
Pull
|
147
|
+
Pull requests are always welcome
|
240
148
|
|
241
149
|
## License
|
242
150
|
|
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.
|
4
|
+
version: 0.7.0
|
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: 2024-
|
11
|
+
date: 2024-11-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '7.0'
|
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: '
|
26
|
+
version: '7.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: zeitwerk
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
75
|
- !ruby/object:Gem::Version
|
76
76
|
version: '0'
|
77
77
|
requirements: []
|
78
|
-
rubygems_version: 3.5.
|
78
|
+
rubygems_version: 3.5.23
|
79
79
|
signing_key:
|
80
80
|
specification_version: 4
|
81
81
|
summary: Multithread is hard
|