asynchronous 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.0"
12
+ gem "jeweler", "~> 1.8.7"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,72 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (4.0.0)
5
+ i18n (~> 0.6, >= 0.6.4)
6
+ minitest (~> 4.2)
7
+ multi_json (~> 1.3)
8
+ thread_safe (~> 0.1)
9
+ tzinfo (~> 0.3.37)
10
+ addressable (2.3.5)
11
+ atomic (1.1.14)
12
+ builder (3.2.2)
13
+ faraday (0.8.8)
14
+ multipart-post (~> 1.2.0)
15
+ git (1.2.6)
16
+ github_api (0.10.1)
17
+ addressable
18
+ faraday (~> 0.8.1)
19
+ hashie (>= 1.2)
20
+ multi_json (~> 1.4)
21
+ nokogiri (~> 1.5.2)
22
+ oauth2
23
+ hashie (2.0.5)
24
+ highline (1.6.20)
25
+ httpauth (0.2.0)
26
+ i18n (0.6.5)
27
+ jeweler (1.8.8)
28
+ builder
29
+ bundler (~> 1.0)
30
+ git (>= 1.2.5)
31
+ github_api (= 0.10.1)
32
+ highline (>= 1.6.15)
33
+ nokogiri (= 1.5.10)
34
+ rake
35
+ rdoc
36
+ json (1.8.1)
37
+ jwt (0.1.8)
38
+ multi_json (>= 1.5)
39
+ minitest (4.7.5)
40
+ multi_json (1.8.2)
41
+ multi_xml (0.5.5)
42
+ multipart-post (1.2.0)
43
+ nokogiri (1.5.10)
44
+ oauth2 (0.9.2)
45
+ faraday (~> 0.8)
46
+ httpauth (~> 0.2)
47
+ jwt (~> 0.1.4)
48
+ multi_json (~> 1.0)
49
+ multi_xml (~> 0.5)
50
+ rack (~> 1.2)
51
+ rack (1.5.2)
52
+ rake (10.1.0)
53
+ rdoc (3.12.2)
54
+ json (~> 1.4)
55
+ shoulda (3.5.0)
56
+ shoulda-context (~> 1.0, >= 1.0.1)
57
+ shoulda-matchers (>= 1.4.1, < 3.0)
58
+ shoulda-context (1.1.5)
59
+ shoulda-matchers (2.4.0)
60
+ activesupport (>= 3.0.0)
61
+ thread_safe (0.1.3)
62
+ atomic
63
+ tzinfo (0.3.38)
64
+
65
+ PLATFORMS
66
+ ruby
67
+
68
+ DEPENDENCIES
69
+ bundler (~> 1.0)
70
+ jeweler (~> 1.8.7)
71
+ rdoc (~> 3.12)
72
+ shoulda
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Adam Luzsi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Adam Luzsi
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ asynchronous
2
+ ============
3
+
4
+ Asynchronous Patterns for Ruby Based on Pure MRI CRuby code
5
+
6
+ # Examples
7
+
8
+ the "simple async processing" will let you use os threads (1.9.n+)
9
+ for multiprocessing so you can give multiple task to do and
10
+ until you ask for the value, the process will be in the background
11
+ You can also use OS threads instead of VM Threads for real Parallelism
12
+
13
+ the "require_files" shows you how can you get files from directory
14
+ in a recursive way and stuffs like that so you can be lay
15
+
16
+ ## LICENSE
17
+
18
+ (The MIT License)
19
+
20
+ Copyright (c) 2009-2013 Adam Luzsi <adamluzsi@gmail.com>
21
+
22
+ Permission is hereby granted, free of charge, to any person obtaining
23
+ a copy of this software and associated documentation files (the
24
+ 'Software'), to deal in the Software without restriction, including
25
+ without limitation the rights to use, copy, modify, merge, publish,
26
+ distribute, sublicense, and/or sell copies of the Software, and to
27
+ permit persons to whom the Software is furnished to do so, subject to
28
+ the following conditions:
29
+
30
+ The above copyright notice and this permission notice shall be
31
+ included in all copies or substantial portions of the Software.
32
+
33
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
34
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
35
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
36
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
37
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
38
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
39
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require File.join "bundler","gem_tasks"
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+
3
+ require File.expand_path(File.join(File.dirname(__FILE__),"files.rb"))
4
+
5
+ ### Specification for the new Gem
6
+ Gem::Specification.new do |spec|
7
+
8
+ spec.name = "asynchronous"
9
+ spec.version = File.open(File.join(File.dirname(__FILE__),"VERSION")).read.split("\n")[0].chomp.gsub(' ','')
10
+ spec.authors = ["Adam Luzsi"]
11
+ spec.email = ["adamluzsi@gmail.com"]
12
+ spec.description = %q{Async dsl for easy concurrency patterns in both VM managed and OS managed way (Concurrency and Parallelism) }
13
+ spec.summary = %q{Simple Async Based on standard CRuby}
14
+ spec.homepage = "https://github.com/adamluzsi/asynchronous"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = SpecFiles
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ ##=======Runtime-ENV================##
23
+ #spec.add_runtime_dependency "asdf", ['~>4.1.3']
24
+
25
+ ##=======Development-ENV============##
26
+ #spec.add_development_dependency "asdf",['~>4.1.3']
27
+
28
+ end
@@ -0,0 +1,56 @@
1
+ # Require Gemfile gems
2
+ require_relative "../lib/asynchronous"
3
+
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
10
+
11
+ sleep 2
12
+ 4 * 2
13
+
14
+ end
15
+ puts "hello concurrency"
16
+
17
+ calculation.value += 1
18
+
19
+ puts calculation.value
20
+
21
+ #>--------------------------------------------------
22
+ # or you can use simple {} without sym and this will be by default a
23
+ # :concurrency pattern
24
+
25
+ calculation = async { sleep 3; 4 * 3 }
26
+
27
+ puts "hello simple concurrency"
28
+
29
+ calculation.value += 1
30
+
31
+ # remember you have to use .value to cal the return value from the code block!
32
+ puts calculation.value
33
+
34
+
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"
51
+
52
+ calculation.value += 1
53
+
54
+ puts calculation.value
55
+
56
+ #>--------------------------------------------------
data/files.rb ADDED
@@ -0,0 +1,24 @@
1
+ ### Get Files from dir
2
+ begin
3
+
4
+ files_to_be_loaded = %w[version.rb]
5
+
6
+ SpecFiles= Array.new
7
+
8
+ Dir[File.expand_path(File.join(File.dirname(__FILE__),"**","*"))].sort.uniq.each do |one_file_name|
9
+ one_file_name = File.expand_path one_file_name
10
+ file_name = one_file_name[(File.expand_path(File.dirname(__FILE__)).to_s.length+1)..(one_file_name.length-1)]
11
+
12
+ if !one_file_name.include?("pkg")
13
+ if !File.directory? file_name
14
+ SpecFiles.push file_name
15
+ STDOUT.puts file_name if $DEBUG
16
+ if files_to_be_loaded.include? one_file_name.split(File::SEPARATOR).last
17
+ load one_file_name
18
+ end
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,9 @@
1
+ #encoding: UTF-8
2
+ module Asynchronous
3
+
4
+ require File.join(File.dirname(__FILE__),"asynchronous","clean_class")
5
+ require File.join(File.dirname(__FILE__),"asynchronous","concurrency")
6
+ require File.join(File.dirname(__FILE__),"asynchronous","parallelism")
7
+ require File.join(File.dirname(__FILE__),"asynchronous","kernel")
8
+
9
+ 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
@@ -0,0 +1,58 @@
1
+ module Asynchronous
2
+
3
+ # you can use simple :c also instead of :concurrency
4
+ # remember :concurrency is all about GIL case, so
5
+ # you can modify the objects in memory
6
+ # This is ideal for little operations in simultaneously or
7
+ # when you need to update objects in the memory
8
+ class Concurrency < CleanClass
9
+
10
+ def initialize(callable)
11
+ begin
12
+ @value= nil
13
+ @rescue_state= nil
14
+ @thread ||= ::Thread.new { callable.call }
15
+ @rescue_state= nil
16
+ rescue ThreadError
17
+ @rescue_state ||= true
18
+ sleep 5
19
+ retry
20
+ end
21
+ end
22
+
23
+ def value
24
+
25
+ if @value.nil?
26
+ until @rescue_state.nil?
27
+ sleep 1
28
+ end
29
+ @value= @thread.value
30
+ end
31
+
32
+ return @value
33
+
34
+ end
35
+
36
+ def value=(obj)
37
+ @value= obj
38
+ end
39
+
40
+ def inspect
41
+ if @thread.alive?
42
+ "#<Async running>"
43
+ else
44
+ value.inspect
45
+ end
46
+ end
47
+
48
+ def method_missing(method, *args)
49
+ value.__send__(method, *args)
50
+ end
51
+
52
+ def respond_to_missing?(method, include_private = false)
53
+ value.respond_to?(method, include_private)
54
+ end
55
+
56
+ end
57
+
58
+ end
@@ -0,0 +1,30 @@
1
+ # kernel method for asyncron calls
2
+ # basic version is :
3
+ #
4
+ # var= async { "ruby Code here" }
5
+ # var.value #> "ruby Code here"
6
+ #
7
+ # or
8
+ #
9
+ # var = async :parallelism do
10
+ # "some awsome ruby code here!"
11
+ # end
12
+ #
13
+ module Kernel
14
+ def async(type= :Concurrency ,&block)
15
+ type= type.to_s
16
+ case type.downcase[0]
17
+ when "c"
18
+ begin
19
+ Asynchronous::Concurrency.new(block)
20
+ end
21
+ when "p"
22
+ begin
23
+ Asynchronous::Parallelism.new(block)
24
+ end
25
+ else
26
+ nil
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,88 @@
1
+ module Asynchronous
2
+ # now let's see the Parallelism
3
+ # you can use simple :p also instead of :parallelism
4
+ # remember :parallelism is all about real OS thread case, so
5
+ # you CANT modify the objects in memory only copy on write modify
6
+ # This is ideal for big operations where you need do a big process
7
+ # and only get the return value so you can do big works without the fear of the
8
+ # Garbage collector slowness or the GIL lock
9
+ # when you need to update objects in the memory use :concurrency
10
+ class Parallelism < CleanClass
11
+
12
+ # Basic
13
+ begin
14
+
15
+ #require 'yaml'
16
+ @@pids=[]
17
+ def initialize callable
18
+
19
+ @value= nil
20
+ @rd, @wr = ::IO.pipe
21
+ @pid= ::Kernel.fork do
22
+
23
+ ::Kernel.trap("TERM") do
24
+ exit
25
+ end
26
+
27
+ @rd.close
28
+ @wr.write ::Marshal.dump(callable.call)#.to_yaml
29
+ @wr.close
30
+
31
+ end
32
+
33
+ @@pids.push(@pid)
34
+
35
+ end
36
+
37
+ end
38
+
39
+ # return value
40
+ begin
41
+
42
+ def value
43
+
44
+ if @value.nil?
45
+
46
+ @wr.close
47
+ return_value= @rd.read
48
+ return_value= ::Marshal.load(return_value)
49
+ @rd.close
50
+ @@pids.delete(@pid)
51
+ @value= return_value
52
+
53
+ end
54
+
55
+ return @value
56
+
57
+ end
58
+
59
+
60
+ def value=(obj)
61
+ @value= obj
62
+ end
63
+
64
+ #def method_missing(method,*args)
65
+ # value.__send__(method,*args)
66
+ #end
67
+ #
68
+ #def respond_to_missing?(method, include_private = false)
69
+ # value.respond_to?(method, include_private)
70
+ #end
71
+
72
+ end
73
+
74
+ # kill Zombies at Kernel Exit
75
+ begin
76
+ ::Kernel.at_exit {
77
+ @@pids.each { |pid|
78
+ begin
79
+ ::Process.kill(:TERM, pid)
80
+ rescue ::Errno::ESRCH, ::Errno::ECHILD
81
+ ::STDOUT.puts "`kill': No such process (Errno::ESRCH)"
82
+ end
83
+ }
84
+ }
85
+ end
86
+
87
+ end
88
+ end
data/test/test.rb ADDED
@@ -0,0 +1,3 @@
1
+ # Require Gemfile gems
2
+ require_relative "../lib/asynchronous"
3
+
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: asynchronous
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adam Luzsi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-01-10 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! 'Async dsl for easy concurrency patterns in both VM managed and OS
15
+ managed way (Concurrency and Parallelism) '
16
+ email:
17
+ - adamluzsi@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - VERSION
29
+ - asynchronous.gemspec
30
+ - examples/async_patterns.rb
31
+ - files.rb
32
+ - lib/asynchronous.rb
33
+ - lib/asynchronous/clean_class.rb
34
+ - lib/asynchronous/concurrency.rb
35
+ - lib/asynchronous/kernel.rb
36
+ - lib/asynchronous/parallelism.rb
37
+ - test/test.rb
38
+ homepage: https://github.com/adamluzsi/asynchronous
39
+ licenses:
40
+ - MIT
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.25
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Simple Async Based on standard CRuby
63
+ test_files:
64
+ - test/test.rb
65
+ has_rdoc: