aw 0.1.12 → 0.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +17 -19
- data/lib/aw.rb +11 -7
- data/lib/aw/fork.rb +17 -6
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9af984fb0d47a9566b6ca5c9ac4a7208328cb444b504865e4fff631bb4122db7
|
4
|
+
data.tar.gz: 8c9759ef06861c6518cedd5867d85ebb04b62a01512a42abd94b401a3d67aa92
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b950326ea7dc9a6846f079e75325e14d90d0261466dbd896dc57644eba8ef08ef8935364e756b6e41ed9f8b11cef68bb9752c9d3ad146daaf375f0b5334920d3
|
7
|
+
data.tar.gz: f21141ba62144f1b86ebead8c2816625c243272c51e671fcbdf95a28b8f81c8716dae4e7063427722094db3e661e8ceca2fdf757f2bf26612f9673dc35754b26
|
data/README.md
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
# Aw
|
2
2
|
|
3
|
-
[](https://github.com/fixrb/aw/releases)
|
4
|
+
[](https://rubydoc.info/github/fixrb/aw/main)
|
5
|
+
[](https://github.com/fixrb/aw/actions?query=workflow%3Aci+branch%3Amain)
|
6
|
+
[](https://github.com/fixrb/aw/actions?query=workflow%3Arubocop+branch%3Amain)
|
7
|
+
[](https://github.com/fixrb/aw/raw/main/LICENSE.md)
|
8
8
|
|
9
9
|
> Aw, fork! 😬
|
10
10
|
|
11
|
-
Creates a
|
11
|
+
Creates a sub-process to execute a block inside, and return the result.
|
12
12
|
|
13
13
|
## Installation
|
14
14
|
|
@@ -20,21 +20,25 @@ gem "aw"
|
|
20
20
|
|
21
21
|
And then execute:
|
22
22
|
|
23
|
-
|
23
|
+
```sh
|
24
|
+
bundle
|
25
|
+
```
|
24
26
|
|
25
27
|
Or install it yourself as:
|
26
28
|
|
27
|
-
|
29
|
+
```sh
|
30
|
+
gem install aw
|
31
|
+
```
|
28
32
|
|
29
33
|
## Usage
|
30
34
|
|
31
|
-
|
35
|
+
Execute a block of code in a sub-process, and return the result to the current process:
|
32
36
|
|
33
37
|
```ruby
|
34
38
|
Aw.fork! { 6 * 7 } # => 42
|
35
39
|
```
|
36
40
|
|
37
|
-
|
41
|
+
Therefore, when the execution of a block of code causes side effects, they are limited to the sub-process:
|
38
42
|
|
39
43
|
```ruby
|
40
44
|
arr = ["foo"]
|
@@ -44,7 +48,7 @@ Aw.fork! { arr << "FUU" } # => ["foo", "FUU"]
|
|
44
48
|
arr # => ["foo"]
|
45
49
|
```
|
46
50
|
|
47
|
-
Exceptions raised within
|
51
|
+
Exceptions raised within a block of code are propagated:
|
48
52
|
|
49
53
|
```ruby
|
50
54
|
Aw.fork! { nil + 1 } # => NoMethodError (undefined method `+' for nil:NilClass)
|
@@ -60,19 +64,13 @@ __Aw__ follows [Semantic Versioning 2.0](https://semver.org/).
|
|
60
64
|
|
61
65
|
## License
|
62
66
|
|
63
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
67
|
+
The [gem](https://rubygems.org/gems/aw) is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
64
68
|
|
65
69
|
***
|
66
70
|
|
67
71
|
<p>
|
68
72
|
This project is sponsored by:<br />
|
69
73
|
<a href="https://sashite.com/"><img
|
70
|
-
src="https://github.com/fixrb/aw/raw/
|
74
|
+
src="https://github.com/fixrb/aw/raw/main/img/sashite.png"
|
71
75
|
alt="Sashite" /></a>
|
72
76
|
</p>
|
73
|
-
|
74
|
-
[gem]: https://rubygems.org/gems/aw
|
75
|
-
[travis]: https://travis-ci.org/fixrb/aw
|
76
|
-
[codeclimate]: https://codeclimate.com/github/fixrb/aw
|
77
|
-
[inchpages]: https://inch-ci.org/github/fixrb/aw
|
78
|
-
[rubydoc]: https://rubydoc.info/gems/aw
|
data/lib/aw.rb
CHANGED
@@ -2,18 +2,22 @@
|
|
2
2
|
|
3
3
|
# Namespace for the Aw library.
|
4
4
|
#
|
5
|
-
# @
|
6
|
-
#
|
7
|
-
# @example Fork and return 42 from 6 * 7.
|
5
|
+
# @example Computes `6 * 7` in a sub-process and returns `42` to the current process.
|
8
6
|
# Aw.fork! { 6 * 7 } # => 42
|
7
|
+
#
|
8
|
+
# @api public
|
9
9
|
module Aw
|
10
|
-
#
|
10
|
+
# Runs the block inside a sub-process, and returns the computed value.
|
11
|
+
#
|
12
|
+
# @param block [Proc] The code to run in a sub-process.
|
11
13
|
#
|
12
|
-
# @
|
14
|
+
# @example Computes `6 * 7` in a sub-process and returns `42` to the current process.
|
15
|
+
# fork! { 6 * 7 } # => 42
|
13
16
|
#
|
14
|
-
# @return [#object_id] The
|
17
|
+
# @return [#object_id] The computed value.
|
15
18
|
def self.fork!(&block)
|
16
|
-
|
19
|
+
read, write = ::IO.pipe
|
20
|
+
Fork.new(read, write).call(&block)
|
17
21
|
end
|
18
22
|
end
|
19
23
|
|
data/lib/aw/fork.rb
CHANGED
@@ -4,6 +4,8 @@ require "English"
|
|
4
4
|
|
5
5
|
module Aw
|
6
6
|
# The Fork class.
|
7
|
+
#
|
8
|
+
# @api private
|
7
9
|
class Fork
|
8
10
|
# Initialize the class.
|
9
11
|
#
|
@@ -27,26 +29,33 @@ module Aw
|
|
27
29
|
# @return [IO] The write endpoint.
|
28
30
|
attr_reader :write
|
29
31
|
|
30
|
-
#
|
32
|
+
# Runs the block inside a sub-process, and returns the computed value.
|
31
33
|
#
|
32
|
-
# @
|
33
|
-
|
34
|
+
# @example Computes `6 * 7` in a sub-process and returns `42` to the current process.
|
35
|
+
# call { 6 * 7 } # => 42
|
36
|
+
#
|
37
|
+
# @return [#object_id] The computed value.
|
38
|
+
def call(&block)
|
34
39
|
pid = fork_and_return_pid(&block)
|
35
40
|
write.close
|
36
41
|
result = read.read
|
37
42
|
::Process.wait(pid)
|
38
43
|
|
39
44
|
# rubocop:disable Security/MarshalLoad
|
40
|
-
::Marshal.load(result).tap
|
41
|
-
raise r if r.is_a?(::Exception)
|
42
|
-
end
|
45
|
+
::Marshal.load(result).tap { |r| raise r if r.is_a?(::Exception) }
|
43
46
|
# rubocop:enable Security/MarshalLoad
|
44
47
|
end
|
45
48
|
|
46
49
|
private
|
47
50
|
|
51
|
+
# Creates a sub-process to execute a block inside, and returns the
|
52
|
+
# sub-process ID.
|
53
|
+
#
|
54
|
+
# @return [Integer] The ID of the created sub-process.
|
48
55
|
def fork_and_return_pid
|
49
56
|
fork do
|
57
|
+
# :nocov:
|
58
|
+
|
50
59
|
read.close
|
51
60
|
|
52
61
|
# rubocop:disable Lint/RescueException
|
@@ -59,6 +68,8 @@ module Aw
|
|
59
68
|
|
60
69
|
::Marshal.dump(result, write)
|
61
70
|
exit!(true)
|
71
|
+
|
72
|
+
# :nocov:
|
62
73
|
end
|
63
74
|
end
|
64
75
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aw
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.13
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cyril Kato
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-07-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -122,7 +122,7 @@ dependencies:
|
|
122
122
|
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
|
-
description: Creates a
|
125
|
+
description: Creates a sub-process to execute a block inside, and returns the result.
|
126
126
|
email: contact@cyril.email
|
127
127
|
executables: []
|
128
128
|
extensions: []
|
@@ -151,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
151
151
|
- !ruby/object:Gem::Version
|
152
152
|
version: '0'
|
153
153
|
requirements: []
|
154
|
-
rubygems_version: 3.1.
|
154
|
+
rubygems_version: 3.1.6
|
155
155
|
signing_key:
|
156
156
|
specification_version: 4
|
157
157
|
summary: "Aw, fork! \U0001F62C"
|