asynchronous 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/examples/async_patterns.rb +20 -1
- data/examples/no_zombie_test.rb +26 -0
- data/lib/asynchronous/clean_class.rb +1 -0
- data/lib/asynchronous/concurrency.rb +9 -0
- data/lib/asynchronous/parallelism.rb +139 -14
- metadata +3 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.1
|
data/examples/async_patterns.rb
CHANGED
@@ -53,4 +53,23 @@ calculation.value += 1
|
|
53
53
|
|
54
54
|
puts calculation.value
|
55
55
|
|
56
|
-
#>--------------------------------------------------
|
56
|
+
#>--------------------------------------------------
|
57
|
+
|
58
|
+
# more complex way
|
59
|
+
|
60
|
+
puts "mixed usecase with arrays as return obj"
|
61
|
+
calc1 = async :parallelism do
|
62
|
+
|
63
|
+
sleep 4
|
64
|
+
# some big database processing brutal memory eater stuff
|
65
|
+
[4*5,"hy"]
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
calc2 = async {
|
70
|
+
[5+1,"sup!"]
|
71
|
+
}
|
72
|
+
|
73
|
+
puts calc1.value == calc2.value
|
74
|
+
puts (calc1.value+calc2.value).inspect
|
75
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative "../lib/asynchronous"
|
2
|
+
|
3
|
+
calculation = async :parallelism do
|
4
|
+
|
5
|
+
# Zombie!
|
6
|
+
loop do
|
7
|
+
sleep 1
|
8
|
+
end
|
9
|
+
# inf loop
|
10
|
+
#
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
puts $$
|
15
|
+
Thread.new do
|
16
|
+
sleep 5
|
17
|
+
|
18
|
+
# this want to demonstrate that,
|
19
|
+
# if the main process is killed,
|
20
|
+
# you wont leave zombies behind!
|
21
|
+
system "kill -9 #{$$}"
|
22
|
+
end
|
23
|
+
|
24
|
+
loop do
|
25
|
+
sleep 1
|
26
|
+
end
|
@@ -9,28 +9,145 @@ module Asynchronous
|
|
9
9
|
# when you need to update objects in the memory use :concurrency
|
10
10
|
class Parallelism < CleanClass
|
11
11
|
|
12
|
-
# Basic
|
12
|
+
# Basic class variables
|
13
13
|
begin
|
14
|
-
@@pids=[]
|
15
|
-
def initialize callable
|
16
14
|
|
17
|
-
|
18
|
-
|
19
|
-
|
15
|
+
@@pids ||= []
|
16
|
+
@@tmpdir ||= nil
|
17
|
+
@@motherpid ||= $$
|
18
|
+
@@agent ||= nil
|
19
|
+
@@zombie ||= true
|
20
20
|
|
21
|
-
|
22
|
-
exit
|
23
|
-
end
|
21
|
+
end
|
24
22
|
|
25
|
-
|
26
|
-
|
27
|
-
|
23
|
+
# main def for logic
|
24
|
+
begin
|
25
|
+
def initialize callable
|
26
|
+
|
27
|
+
# defaults
|
28
|
+
begin
|
29
|
+
@value= nil
|
30
|
+
@rd, @wr = ::IO.pipe
|
31
|
+
end
|
32
|
+
|
33
|
+
# create a process
|
34
|
+
begin
|
35
|
+
@pid= ::Kernel.fork do
|
36
|
+
|
37
|
+
# anti zombie
|
38
|
+
begin
|
39
|
+
::Kernel.trap("TERM") do
|
40
|
+
::Kernel.exit
|
41
|
+
end
|
42
|
+
::Thread.new do
|
43
|
+
::Kernel.loop do
|
44
|
+
begin
|
45
|
+
::Kernel.sleep 1
|
46
|
+
if mother? == false
|
47
|
+
::Kernel.exit!
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# return the value
|
55
|
+
begin
|
56
|
+
@rd.close
|
57
|
+
@wr.write ::Marshal.dump(callable.call)
|
58
|
+
@wr.close
|
59
|
+
end
|
28
60
|
|
61
|
+
end
|
62
|
+
@@pids.push(@pid)
|
29
63
|
end
|
30
64
|
|
31
|
-
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# connection for in case of mother die
|
69
|
+
begin
|
32
70
|
|
71
|
+
#def tmpdir
|
72
|
+
#
|
73
|
+
# ::Kernel.require "tmpdir"
|
74
|
+
# @@tmpdir= ::File.join(::Dir.tmpdir,('asynchronous'))
|
75
|
+
# unless ::File.directory?(@@tmpdir)
|
76
|
+
# ::Dir.mkdir(@@tmpdir)
|
77
|
+
# end
|
78
|
+
#
|
79
|
+
# %w[ signal ].each do |one_str|
|
80
|
+
# unless ::File.directory?(::File.join(@@tmpdir,one_str))
|
81
|
+
# ::Dir.mkdir(::File.join(@@tmpdir,one_str))
|
82
|
+
# end
|
83
|
+
# end
|
84
|
+
#
|
85
|
+
# # pidnamed tmp file for tracking
|
86
|
+
# unless ::File.exist?(::File.join(@@tmpdir,'signal',@@motherpid.to_s))
|
87
|
+
# ::File.new(::File.join(@@tmpdir,'signal',@@motherpid.to_s),"w").write('')
|
88
|
+
# end
|
89
|
+
#
|
90
|
+
#end
|
91
|
+
#
|
92
|
+
#def tmp_write_agent
|
93
|
+
# if @@agent != true
|
94
|
+
# ::Thread.new do
|
95
|
+
# ::Kernel.loop do
|
96
|
+
# ::File.open(::File.join(@@tmpdir,"signal",@@motherpid.to_s),"w") do |file|
|
97
|
+
# file.write( ::Time.now.to_i.to_s )
|
98
|
+
# end
|
99
|
+
# sleep 3
|
100
|
+
# end
|
101
|
+
# end
|
102
|
+
# @@agent ||= true
|
103
|
+
# end
|
104
|
+
#end
|
105
|
+
#
|
106
|
+
#def tmp_read
|
107
|
+
#
|
108
|
+
# counter= 0
|
109
|
+
# begin
|
110
|
+
#
|
111
|
+
# ::Kernel.loop do
|
112
|
+
# return_string= ::File.open(
|
113
|
+
# ::File.join(@@tmpdir,"signal",@@motherpid.to_s),
|
114
|
+
# ::File::RDONLY
|
115
|
+
# ).read
|
116
|
+
#
|
117
|
+
# if !return_string.nil? && return_string != ""
|
118
|
+
# return return_string
|
119
|
+
# else
|
120
|
+
# if counter > 5
|
121
|
+
# return nil
|
122
|
+
# else
|
123
|
+
# counter += 1
|
124
|
+
# ::Kernel.sleep(1)
|
125
|
+
# end
|
126
|
+
# end
|
127
|
+
#
|
128
|
+
# end
|
129
|
+
#
|
130
|
+
# rescue ::IOError
|
131
|
+
# if counter > 5
|
132
|
+
# return nil
|
133
|
+
# else
|
134
|
+
# counter += 1
|
135
|
+
# end
|
136
|
+
# ::Kernel.sleep 1
|
137
|
+
# retry
|
138
|
+
# end
|
139
|
+
#
|
140
|
+
#end
|
141
|
+
|
142
|
+
def mother?
|
143
|
+
begin
|
144
|
+
::Process.kill(0,@@motherpid)
|
145
|
+
return true
|
146
|
+
rescue ::Errno::ESRCH
|
147
|
+
return false
|
148
|
+
end
|
33
149
|
end
|
150
|
+
|
34
151
|
end
|
35
152
|
|
36
153
|
# return value
|
@@ -41,7 +158,6 @@ module Asynchronous
|
|
41
158
|
if @value.nil?
|
42
159
|
|
43
160
|
@wr.close
|
44
|
-
return_value= @rd.read
|
45
161
|
return_value= ::Marshal.load(return_value)
|
46
162
|
@rd.close
|
47
163
|
@@pids.delete(@pid)
|
@@ -72,5 +188,14 @@ module Asynchronous
|
|
72
188
|
}
|
73
189
|
end
|
74
190
|
|
191
|
+
# alias
|
192
|
+
begin
|
193
|
+
alias :v :value
|
194
|
+
#alias :get :value
|
195
|
+
#alias :gets :value
|
196
|
+
#alias :response :value
|
197
|
+
#alias :return :value
|
198
|
+
end
|
199
|
+
|
75
200
|
end
|
76
201
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: asynchronous
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-01-
|
12
|
+
date: 2014-01-13 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! 'DSL for for dead simple to use asynchronous patterns in both VM managed
|
15
15
|
and OS managed way (Concurrency and Parallelism) '
|
@@ -28,6 +28,7 @@ files:
|
|
28
28
|
- VERSION
|
29
29
|
- asynchronous.gemspec
|
30
30
|
- examples/async_patterns.rb
|
31
|
+
- examples/no_zombie_test.rb
|
31
32
|
- files.rb
|
32
33
|
- lib/asynchronous.rb
|
33
34
|
- lib/asynchronous/clean_class.rb
|