procemon 0.3.4 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -0
- data/VERSION +1 -1
- data/examples/simple_async_processing.rb +38 -22
- data/lib/procemon.rb +1 -1
- data/lib/procemon/function/async/{async.rb → concurrency.rb} +21 -17
- data/lib/procemon/function/async/kernel.rb +15 -2
- data/lib/procemon/function/async/parallelism.rb +89 -0
- data/lib/procemon/mpatch/clean.rb +18 -0
- data/procemon.gemspec +1 -1
- data/test/test.rb +0 -19
- metadata +9 -7
- data/lib/procemon/function/async/safe_thread.rb +0 -15
data/README.md
CHANGED
@@ -32,6 +32,7 @@ with the right bindings
|
|
32
32
|
the "simple async processing" will let you use os threads (1.9.n+)
|
33
33
|
for multiprocessing so you can give multiple task to do and
|
34
34
|
until you ask for the value, the process will be in the background
|
35
|
+
You can also use OS threads instead of VM Threads for real Parallelism
|
35
36
|
|
36
37
|
the "require_files" shows you how can you get files from directory
|
37
38
|
in a recursive way and stuffs like that so you can be lay
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
@@ -1,40 +1,56 @@
|
|
1
1
|
# Require Gemfile gems
|
2
2
|
require_relative "../lib/procemon"
|
3
3
|
|
4
|
-
|
4
|
+
# you can use simple :c also instead of :concurrency
|
5
|
+
# remember :concurrency is all about GIL case, so
|
6
|
+
# you can modify the objects in memory
|
7
|
+
# This is ideal for little operations in simultaneously or
|
8
|
+
# when you need to update objects in the memory
|
9
|
+
calculation = async :concurrency do
|
5
10
|
|
6
|
-
sleep
|
7
|
-
4 *
|
8
|
-
}
|
11
|
+
sleep 2
|
12
|
+
4 * 2
|
9
13
|
|
10
|
-
|
14
|
+
end
|
15
|
+
puts "hello concurrency"
|
11
16
|
|
12
|
-
calculation += 1
|
17
|
+
calculation.value += 1
|
13
18
|
|
14
|
-
puts calculation
|
19
|
+
puts calculation.value
|
15
20
|
|
16
21
|
#>--------------------------------------------------
|
22
|
+
# or you can use simple {} without sym and this will be by default a
|
23
|
+
# :concurrency pattern
|
17
24
|
|
25
|
+
calculation = async { sleep 3; 4 * 3 }
|
18
26
|
|
19
|
-
|
27
|
+
puts "hello simple concurrency"
|
20
28
|
|
21
|
-
|
22
|
-
hello= 14
|
23
|
-
sup= "the world is yours"
|
29
|
+
calculation.value += 1
|
24
30
|
|
25
|
-
|
31
|
+
# remember you have to use .value to cal the return value from the code block!
|
32
|
+
puts calculation.value
|
26
33
|
|
27
|
-
}
|
28
34
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
35
|
+
#>--------------------------------------------------
|
36
|
+
# now let's see the Parallelism
|
37
|
+
# you can use simple :p also instead of :parallelism
|
38
|
+
# remember :parallelism is all about real OS thread case, so
|
39
|
+
# you CANT modify the objects in memory only copy on write modify
|
40
|
+
# This is ideal for big operations where you need do a big process
|
41
|
+
# and only get the return value so you can do big works without the fear of the
|
42
|
+
# Garbage collector slowness or the GIL lock
|
43
|
+
# when you need to update objects in the memory use :concurrency
|
44
|
+
calculation = async :parallelism do
|
45
|
+
|
46
|
+
sleep 4
|
47
|
+
4 * 5
|
48
|
+
|
49
|
+
end
|
50
|
+
puts "hello parallelism"
|
33
51
|
|
34
|
-
|
52
|
+
calculation.value += 1
|
35
53
|
|
36
|
-
|
37
|
-
puts asd
|
54
|
+
puts calculation.value
|
38
55
|
|
39
|
-
|
40
|
-
puts Time.now-start_time
|
56
|
+
#>--------------------------------------------------
|
data/lib/procemon.rb
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
module Procemon
|
3
3
|
|
4
4
|
require File.join(File.dirname(__FILE__),"procemon","function","require")
|
5
|
-
require_relative_directory File.join("procemon","function")
|
6
5
|
require_relative_directory File.join("procemon","mpatch")
|
6
|
+
require_relative_directory File.join("procemon","function")
|
7
7
|
|
8
8
|
# load up helpers
|
9
9
|
#Dir.glob(\
|
@@ -1,18 +1,13 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
:object_id,
|
8
|
-
:__send__,
|
9
|
-
:new
|
10
|
-
]).each do |method|
|
11
|
-
undef_method method
|
12
|
-
end
|
1
|
+
# you can use simple :c also instead of :concurrency
|
2
|
+
# remember :concurrency is all about GIL case, so
|
3
|
+
# you can modify the objects in memory
|
4
|
+
# This is ideal for little operations in simultaneously or
|
5
|
+
# when you need to update objects in the memory
|
6
|
+
class Concurrency < CleanClass
|
13
7
|
|
14
8
|
def initialize(callable)
|
15
9
|
begin
|
10
|
+
@value= nil
|
16
11
|
@rescue_state= nil
|
17
12
|
@thread ||= ::Thread.new { callable.call }
|
18
13
|
@rescue_state= nil
|
@@ -24,11 +19,20 @@ class Async #< BasicObject
|
|
24
19
|
end
|
25
20
|
|
26
21
|
def value
|
27
|
-
|
28
|
-
|
29
|
-
|
22
|
+
|
23
|
+
if @value.nil?
|
24
|
+
until @rescue_state.nil?
|
25
|
+
sleep 1
|
26
|
+
end
|
27
|
+
@value= @thread.value
|
30
28
|
end
|
31
|
-
|
29
|
+
|
30
|
+
return @value
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
def value=(obj)
|
35
|
+
@value= obj
|
32
36
|
end
|
33
37
|
|
34
38
|
def inspect
|
@@ -47,4 +51,4 @@ class Async #< BasicObject
|
|
47
51
|
value.respond_to?(method, include_private)
|
48
52
|
end
|
49
53
|
|
50
|
-
end
|
54
|
+
end
|
@@ -1,5 +1,18 @@
|
|
1
1
|
module Kernel
|
2
|
-
def async(
|
3
|
-
|
2
|
+
def async(type= :Concurrency ,&block)
|
3
|
+
type= type.to_s
|
4
|
+
case type.downcase[0]
|
5
|
+
when "c"
|
6
|
+
begin
|
7
|
+
Concurrency.new(block)
|
8
|
+
end
|
9
|
+
when "p"
|
10
|
+
begin
|
11
|
+
Parallelism.new(block)
|
12
|
+
end
|
13
|
+
else
|
14
|
+
nil
|
15
|
+
|
16
|
+
end
|
4
17
|
end
|
5
18
|
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
# now let's see the Parallelism
|
2
|
+
# you can use simple :p also instead of :parallelism
|
3
|
+
# remember :parallelism is all about real OS thread case, so
|
4
|
+
# you CANT modify the objects in memory only copy on write modify
|
5
|
+
# This is ideal for big operations where you need do a big process
|
6
|
+
# and only get the return value so you can do big works without the fear of the
|
7
|
+
# Garbage collector slowness or the GIL lock
|
8
|
+
# when you need to update objects in the memory use :concurrency
|
9
|
+
class Parallelism < CleanClass
|
10
|
+
|
11
|
+
# Basic
|
12
|
+
begin
|
13
|
+
|
14
|
+
require 'yaml'
|
15
|
+
@@pids=[]
|
16
|
+
def initialize callable
|
17
|
+
|
18
|
+
@value= nil
|
19
|
+
@rd, @wr = ::IO.pipe
|
20
|
+
@pid= ::Kernel.fork do
|
21
|
+
|
22
|
+
::Kernel.trap("TERM") do
|
23
|
+
exit
|
24
|
+
end
|
25
|
+
|
26
|
+
@rd.close
|
27
|
+
@wr.write callable.call.to_yaml
|
28
|
+
@wr.close
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
@@pids.push(@pid)
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
# return value
|
39
|
+
begin
|
40
|
+
|
41
|
+
def value
|
42
|
+
|
43
|
+
if @value.nil?
|
44
|
+
|
45
|
+
@wr.close
|
46
|
+
return_value= @rd.read
|
47
|
+
begin
|
48
|
+
return_value= ::YAML.load(return_value)
|
49
|
+
rescue ::Exception
|
50
|
+
end
|
51
|
+
@rd.close
|
52
|
+
@@pids.delete(@pid)
|
53
|
+
@value= return_value
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
return @value
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
def value=(obj)
|
63
|
+
@value= obj
|
64
|
+
end
|
65
|
+
|
66
|
+
#def method_missing(method,*args)
|
67
|
+
# value.__send__(method,*args)
|
68
|
+
#end
|
69
|
+
#
|
70
|
+
#def respond_to_missing?(method, include_private = false)
|
71
|
+
# value.respond_to?(method, include_private)
|
72
|
+
#end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
# kill Zombies at Kernel Exit
|
77
|
+
begin
|
78
|
+
::Kernel.at_exit {
|
79
|
+
@@pids.each { |pid|
|
80
|
+
begin
|
81
|
+
::Process.kill(:TERM, pid)
|
82
|
+
rescue ::Errno::ESRCH, ::Errno::ECHILD
|
83
|
+
::STDOUT.puts "`kill': No such process (Errno::ESRCH)"
|
84
|
+
end
|
85
|
+
}
|
86
|
+
}
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class CleanClass < BasicObject
|
2
|
+
|
3
|
+
# remove methods from the class!
|
4
|
+
#def self.purge_methods
|
5
|
+
|
6
|
+
(self.instance_methods-[
|
7
|
+
:undef_method,
|
8
|
+
:object_id,
|
9
|
+
:__send__,
|
10
|
+
:methods,
|
11
|
+
:new
|
12
|
+
]).each do |method|
|
13
|
+
undef_method method
|
14
|
+
end
|
15
|
+
|
16
|
+
#end
|
17
|
+
|
18
|
+
end
|
data/procemon.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.version = File.open(File.join(File.dirname(__FILE__),"VERSION")).read.split("\n")[0].chomp.gsub(' ','')
|
10
10
|
spec.authors = ["Adam Luzsi"]
|
11
11
|
spec.email = ["adamluzsi@gmail.com"]
|
12
|
-
spec.description = %q{This is a collection of my Ruby Procs in the adventure of becoming the best! In short this provides extra tools in Application configs, argumens processing,daemonise,eval, getting source of a block,method,process and work with it, or even fuse them into a new proc , require relative files, or directories, string to duck parsing, system tmp_dir using, meta-programing stuffs,async dsl for easy concurrency patterns, micro framework that can be used alongside with any framework and a lot of monkey patch for extra functions :) follow me on github if you like my work! }
|
12
|
+
spec.description = %q{This is a collection of my Ruby Procs in the adventure of becoming the best! In short this provides extra tools in Application configs, argumens processing,daemonise,eval, getting source of a block,method,process and work with it, or even fuse them into a new proc , require relative files, or directories, string to duck parsing, system tmp_dir using, meta-programing stuffs,async dsl for easy concurrency patterns in both VM managed and OS managed way (concurrency and Parallelism), micro framework that can be used alongside with any framework and a lot of monkey patch for extra functions :) follow me on github if you like my work! }
|
13
13
|
spec.summary = %q{Gotta catch em all!}
|
14
14
|
spec.homepage = "https://github.com/adamluzsi/procemon"
|
15
15
|
spec.license = "MIT"
|
data/test/test.rb
CHANGED
@@ -1,22 +1,3 @@
|
|
1
1
|
# Require Gemfile gems
|
2
2
|
require_relative "../lib/procemon"
|
3
3
|
|
4
|
-
|
5
|
-
test1 = async {
|
6
|
-
|
7
|
-
sleep 8
|
8
|
-
hello= 14
|
9
|
-
sup= "the world is yours"
|
10
|
-
|
11
|
-
sup
|
12
|
-
|
13
|
-
}
|
14
|
-
|
15
|
-
test2 = async {
|
16
|
-
sleep(4)
|
17
|
-
"world"
|
18
|
-
}
|
19
|
-
|
20
|
-
start_time= Time.now
|
21
|
-
puts test1.value == test2.value
|
22
|
-
puts Time.now-start_time
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: procemon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,15 +9,16 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-01-
|
12
|
+
date: 2014-01-10 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! 'This is a collection of my Ruby Procs in the adventure of becoming
|
15
15
|
the best! In short this provides extra tools in Application configs, argumens processing,daemonise,eval,
|
16
16
|
getting source of a block,method,process and work with it, or even fuse them into
|
17
17
|
a new proc , require relative files, or directories, string to duck parsing, system
|
18
|
-
tmp_dir using, meta-programing stuffs,async dsl for easy concurrency patterns
|
19
|
-
|
20
|
-
|
18
|
+
tmp_dir using, meta-programing stuffs,async dsl for easy concurrency patterns in
|
19
|
+
both VM managed and OS managed way (concurrency and Parallelism), micro framework
|
20
|
+
that can be used alongside with any framework and a lot of monkey patch for extra
|
21
|
+
functions :) follow me on github if you like my work! '
|
21
22
|
email:
|
22
23
|
- adamluzsi@gmail.com
|
23
24
|
executables: []
|
@@ -39,9 +40,9 @@ files:
|
|
39
40
|
- lib/procemon/extra/str2duck.rb
|
40
41
|
- lib/procemon/function/application.rb
|
41
42
|
- lib/procemon/function/argv.rb
|
42
|
-
- lib/procemon/function/async/
|
43
|
+
- lib/procemon/function/async/concurrency.rb
|
43
44
|
- lib/procemon/function/async/kernel.rb
|
44
|
-
- lib/procemon/function/async/
|
45
|
+
- lib/procemon/function/async/parallelism.rb
|
45
46
|
- lib/procemon/function/binding/binding.rb
|
46
47
|
- lib/procemon/function/binding/bindless.rb
|
47
48
|
- lib/procemon/function/daemon.rb
|
@@ -65,6 +66,7 @@ files:
|
|
65
66
|
- lib/procemon/mpatch/array.rb
|
66
67
|
- lib/procemon/mpatch/boolean.rb
|
67
68
|
- lib/procemon/mpatch/class.rb
|
69
|
+
- lib/procemon/mpatch/clean.rb
|
68
70
|
- lib/procemon/mpatch/exception.rb
|
69
71
|
- lib/procemon/mpatch/file.rb
|
70
72
|
- lib/procemon/mpatch/hash.rb
|