aw 0.1.13 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.md +1 -1
- data/README.md +55 -12
- data/lib/aw/fork.rb +6 -5
- data/lib/aw.rb +27 -6
- metadata +10 -120
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e73149e7b499dddb359e9995c9cfe6b1af71d2bb63789f9bf0e13d029893b10
|
4
|
+
data.tar.gz: debe86862bee16a1902fb2c1b6448ba7d5bff733fa5ed0470cce4af5a2d30cb4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 92954fad1db8bae390956d76c27cffb0fe399c55f2df5b16f102a2a69345a09b62d64a0694864036745f4cb4ceb89a7e143547d035da743e3abd22c0d1ffa14a
|
7
|
+
data.tar.gz: a1eb7a51f4bd7c068ace563f093e2ba94235067bd773a7921f6f175cf9a2a559ba045b9b54fe6f021b5b114ad33bdab48bbc0843ef617ea9fe42a386ae52ef79
|
data/LICENSE.md
CHANGED
data/README.md
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
# Aw
|
2
2
|
|
3
|
-
[![Version](https://img.shields.io/github/v/tag/fixrb/aw?label=Version&logo=github)](https://github.com/fixrb/aw/
|
3
|
+
[![Version](https://img.shields.io/github/v/tag/fixrb/aw?label=Version&logo=github)](https://github.com/fixrb/aw/tags)
|
4
4
|
[![Yard documentation](https://img.shields.io/badge/Yard-documentation-blue.svg?logo=github)](https://rubydoc.info/github/fixrb/aw/main)
|
5
|
-
[![
|
5
|
+
[![Ruby](https://github.com/fixrb/aw/workflows/Ruby/badge.svg?branch=main)](https://github.com/fixrb/aw/actions?query=workflow%3Aruby+branch%3Amain)
|
6
6
|
[![RuboCop](https://github.com/fixrb/aw/workflows/RuboCop/badge.svg?branch=main)](https://github.com/fixrb/aw/actions?query=workflow%3Arubocop+branch%3Amain)
|
7
7
|
[![License](https://img.shields.io/github/license/fixrb/aw?label=License&logo=github)](https://github.com/fixrb/aw/raw/main/LICENSE.md)
|
8
8
|
|
9
|
-
> Aw, fork
|
9
|
+
> Aw, fork 😬
|
10
10
|
|
11
|
-
|
11
|
+
![Aw](https://github.com/fixrb/aw/raw/main/img/aw.jpg)
|
12
|
+
|
13
|
+
Creates a sub-process to execute a block inside, and returns what it returns (or a boolean).
|
12
14
|
|
13
15
|
## Installation
|
14
16
|
|
@@ -21,7 +23,7 @@ gem "aw"
|
|
21
23
|
And then execute:
|
22
24
|
|
23
25
|
```sh
|
24
|
-
bundle
|
26
|
+
bundle install
|
25
27
|
```
|
26
28
|
|
27
29
|
Or install it yourself as:
|
@@ -32,26 +34,67 @@ gem install aw
|
|
32
34
|
|
33
35
|
## Usage
|
34
36
|
|
35
|
-
|
37
|
+
To make __Aw__ available:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
require "aw"
|
41
|
+
```
|
42
|
+
|
43
|
+
There are two methods:
|
44
|
+
|
45
|
+
- `fork!`
|
46
|
+
- `fork?`
|
47
|
+
|
48
|
+
### Method `.fork!`
|
49
|
+
|
50
|
+
Executes a block of code in a sub-process, and returns the result:
|
36
51
|
|
37
52
|
```ruby
|
38
53
|
Aw.fork! { 6 * 7 } # => 42
|
39
54
|
```
|
40
55
|
|
41
|
-
|
56
|
+
When the execution of a block of code causes side effects, these are limited to the sub-process:
|
42
57
|
|
43
58
|
```ruby
|
44
|
-
arr = ["foo"]
|
59
|
+
arr = ["foo"] # => ["foo"]
|
45
60
|
|
46
61
|
Aw.fork! { arr << "FUU" } # => ["foo", "FUU"]
|
47
62
|
|
48
63
|
arr # => ["foo"]
|
49
64
|
```
|
50
65
|
|
51
|
-
Exceptions raised
|
66
|
+
Exceptions raised in a block of code are propagated:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
Aw.fork! { nil + 1 }
|
70
|
+
```
|
71
|
+
|
72
|
+
results in the error:
|
73
|
+
|
74
|
+
> `NoMethodError` (undefined method `+' for nil:NilClass)
|
75
|
+
|
76
|
+
### Method `.fork?`
|
77
|
+
|
78
|
+
Executes a block of code in a sub-process, and returns `true` if no exception is thrown:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
Aw.fork? { 6 * 7 } # => true
|
82
|
+
```
|
83
|
+
|
84
|
+
When the execution of a block of code causes side effects, these are limited to the sub-process:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
arr = ["foo"] # => ["foo"]
|
88
|
+
|
89
|
+
Aw.fork? { arr << "FUU" } # => true
|
90
|
+
|
91
|
+
arr # => ["foo"]
|
92
|
+
```
|
93
|
+
|
94
|
+
When an exception is raised in a code block, `false` is returned:
|
52
95
|
|
53
96
|
```ruby
|
54
|
-
Aw.fork
|
97
|
+
Aw.fork? { nil + 1 } # => false
|
55
98
|
```
|
56
99
|
|
57
100
|
## Contact
|
@@ -64,7 +107,7 @@ __Aw__ follows [Semantic Versioning 2.0](https://semver.org/).
|
|
64
107
|
|
65
108
|
## License
|
66
109
|
|
67
|
-
The [gem](https://rubygems.org/gems/aw) is available as open source under the terms of the [MIT License](https://
|
110
|
+
The [gem](https://rubygems.org/gems/aw) is available as open source under the terms of the [MIT License](https://github.com/fixrb/aw/raw/main/LICENSE.md).
|
68
111
|
|
69
112
|
***
|
70
113
|
|
@@ -72,5 +115,5 @@ The [gem](https://rubygems.org/gems/aw) is available as open source under the te
|
|
72
115
|
This project is sponsored by:<br />
|
73
116
|
<a href="https://sashite.com/"><img
|
74
117
|
src="https://github.com/fixrb/aw/raw/main/img/sashite.png"
|
75
|
-
alt="
|
118
|
+
alt="Sashité" /></a>
|
76
119
|
</p>
|
data/lib/aw/fork.rb
CHANGED
@@ -34,9 +34,10 @@ module Aw
|
|
34
34
|
# @example Computes `6 * 7` in a sub-process and returns `42` to the current process.
|
35
35
|
# call { 6 * 7 } # => 42
|
36
36
|
#
|
37
|
-
# @
|
38
|
-
|
39
|
-
|
37
|
+
# @raise [Exception] Exceptions raised in a block of code are propagated.
|
38
|
+
# @return [#object_id] Returns the value that has been returned in the block.
|
39
|
+
def call(&)
|
40
|
+
pid = fork_and_return_pid(&)
|
40
41
|
write.close
|
41
42
|
result = read.read
|
42
43
|
::Process.wait(pid)
|
@@ -53,7 +54,7 @@ module Aw
|
|
53
54
|
#
|
54
55
|
# @return [Integer] The ID of the created sub-process.
|
55
56
|
def fork_and_return_pid
|
56
|
-
fork do
|
57
|
+
::Process.fork do
|
57
58
|
# :nocov:
|
58
59
|
|
59
60
|
read.close
|
@@ -67,7 +68,7 @@ module Aw
|
|
67
68
|
# rubocop:enable Lint/RescueException
|
68
69
|
|
69
70
|
::Marshal.dump(result, write)
|
70
|
-
exit!(true)
|
71
|
+
::Process.exit!(true)
|
71
72
|
|
72
73
|
# :nocov:
|
73
74
|
end
|
data/lib/aw.rb
CHANGED
@@ -5,19 +5,40 @@
|
|
5
5
|
# @example Computes `6 * 7` in a sub-process and returns `42` to the current process.
|
6
6
|
# Aw.fork! { 6 * 7 } # => 42
|
7
7
|
#
|
8
|
+
# @example Computes `6 * 7` in a sub-process and returns `true` to the current process if no exception is thrown.
|
9
|
+
# Aw.fork? { 6 * 7 } # => true
|
10
|
+
#
|
8
11
|
# @api public
|
9
12
|
module Aw
|
10
13
|
# Runs the block inside a sub-process, and returns the computed value.
|
11
14
|
#
|
12
|
-
# @param block [Proc] The code to run in a sub-process.
|
13
|
-
#
|
14
15
|
# @example Computes `6 * 7` in a sub-process and returns `42` to the current process.
|
15
|
-
# fork! { 6 * 7 } # => 42
|
16
|
+
# Aw.fork! { 6 * 7 } # => 42
|
16
17
|
#
|
17
|
-
# @
|
18
|
-
|
18
|
+
# @example Computes `nil + 1` in a sub-process and raises `NoMethodError` to the current process.
|
19
|
+
# Aw.fork! { nil + 1 } # => raise NoMethodError (undefined method `+' for nil:NilClass)
|
20
|
+
#
|
21
|
+
# @raise [Exception] Exceptions raised in a block of code are propagated.
|
22
|
+
# @return [#object_id] Returns the value that has been returned in the block.
|
23
|
+
def self.fork!(&)
|
19
24
|
read, write = ::IO.pipe
|
20
|
-
Fork.new(read, write).call(&
|
25
|
+
Fork.new(read, write).call(&)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Runs the block inside a sub-process, and returns `true` if no exception is
|
29
|
+
# thrown. Otherwise when an exception is raised, `false` is returned.
|
30
|
+
#
|
31
|
+
# @example Computes `6 * 7` in a sub-process and returns `true` to the current process.
|
32
|
+
# Aw.fork? { 6 * 7 } # => true
|
33
|
+
#
|
34
|
+
# @example Computes `nil + 1` in a sub-process and returns `false` to the current process.
|
35
|
+
# Aw.fork? { nil + 1 } # => false
|
36
|
+
#
|
37
|
+
# @return [Boolean] Returns `true` if stat is successful, `false` if not.
|
38
|
+
def self.fork?(&)
|
39
|
+
pid = ::Process.fork(&)
|
40
|
+
_, status = ::Process.wait2(pid)
|
41
|
+
status.success?
|
21
42
|
end
|
22
43
|
end
|
23
44
|
|
metadata
CHANGED
@@ -1,128 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aw
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cyril Kato
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
13
|
-
-
|
14
|
-
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ">="
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: rake
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: rubocop-md
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ">="
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ">="
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: rubocop-performance
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: rubocop-rake
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ">="
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - ">="
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: rubocop-thread_safety
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - ">="
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: '0'
|
90
|
-
type: :development
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
requirements:
|
94
|
-
- - ">="
|
95
|
-
- !ruby/object:Gem::Version
|
96
|
-
version: '0'
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: simplecov
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
100
|
-
requirements:
|
101
|
-
- - ">="
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
version: '0'
|
104
|
-
type: :development
|
105
|
-
prerelease: false
|
106
|
-
version_requirements: !ruby/object:Gem::Requirement
|
107
|
-
requirements:
|
108
|
-
- - ">="
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: '0'
|
111
|
-
- !ruby/object:Gem::Dependency
|
112
|
-
name: yard
|
113
|
-
requirement: !ruby/object:Gem::Requirement
|
114
|
-
requirements:
|
115
|
-
- - ">="
|
116
|
-
- !ruby/object:Gem::Version
|
117
|
-
version: '0'
|
118
|
-
type: :development
|
119
|
-
prerelease: false
|
120
|
-
version_requirements: !ruby/object:Gem::Requirement
|
121
|
-
requirements:
|
122
|
-
- - ">="
|
123
|
-
- !ruby/object:Gem::Version
|
124
|
-
version: '0'
|
125
|
-
description: Creates a sub-process to execute a block inside, and returns the result.
|
11
|
+
date: 2024-01-25 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Creates a sub-process to execute a block inside, and returns what it
|
14
|
+
returns.
|
126
15
|
email: contact@cyril.email
|
127
16
|
executables: []
|
128
17
|
extensions: []
|
@@ -135,7 +24,8 @@ files:
|
|
135
24
|
homepage: https://github.com/fixrb/aw
|
136
25
|
licenses:
|
137
26
|
- MIT
|
138
|
-
metadata:
|
27
|
+
metadata:
|
28
|
+
rubygems_mfa_required: 'true'
|
139
29
|
post_install_message:
|
140
30
|
rdoc_options: []
|
141
31
|
require_paths:
|
@@ -144,15 +34,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
144
34
|
requirements:
|
145
35
|
- - ">="
|
146
36
|
- !ruby/object:Gem::Version
|
147
|
-
version: 2.
|
37
|
+
version: 3.2.0
|
148
38
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
39
|
requirements:
|
150
40
|
- - ">="
|
151
41
|
- !ruby/object:Gem::Version
|
152
42
|
version: '0'
|
153
43
|
requirements: []
|
154
|
-
rubygems_version: 3.
|
44
|
+
rubygems_version: 3.4.19
|
155
45
|
signing_key:
|
156
46
|
specification_version: 4
|
157
|
-
summary: "Aw, fork
|
47
|
+
summary: "Aw, fork \U0001F62C"
|
158
48
|
test_files: []
|