easy_dispatch 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in easy_dispatch.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Ray Hilton
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # EasyDispatch
2
+
3
+ Super simple concurrecy in ruby
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'easy_dispatch'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install easy_dispatch
18
+
19
+ ## Usage
20
+
21
+ ### Apply function to array, concurrently
22
+
23
+ Synonymous to GCD's dispatch_apply:
24
+
25
+ ```ruby
26
+ [0,1,10,20].apply(2) do |i|
27
+ puts i
28
+ end
29
+ ```
30
+
31
+ this will invoke the block for each object in the array, but with a concurrency of 2
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/easy_dispatch/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Ray Hilton"]
6
+ gem.email = ["ray@wirestorm.net"]
7
+ gem.description = %q{Super simple concurrecy in ruby}
8
+ gem.summary = %q{Super simple concurrecy in ruby}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ # gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "easy_dispatch"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = EasyDispatch::VERSION
17
+ end
@@ -0,0 +1 @@
1
+ require "core_ext/array.rb"
@@ -0,0 +1,19 @@
1
+ class Array
2
+ def apply
3
+ pool = ::EasyDispatch::Pool.new concurrency
4
+ mutex = Mutex.new
5
+ index = 0
6
+ self.each do |o|
7
+ pool.dispatch do
8
+ mutex.synchronize do
9
+ index += 1
10
+ end
11
+ # puts "Running job at index #{index}"
12
+ yield(index, o)
13
+ # puts "Finished job at index #{index}"
14
+ end
15
+ end
16
+
17
+ pool.join
18
+ end
19
+ end
@@ -0,0 +1,6 @@
1
+ require "easy_dispatch/version"
2
+ require "easy_dispatch/pool"
3
+ require "core_ext"
4
+
5
+ module EasyDispatch
6
+ end
@@ -0,0 +1,83 @@
1
+ module EasyDispatch
2
+ class Pool
3
+ require 'thread'
4
+
5
+ def initialize size
6
+ @running = true
7
+ @queue = Queue.new #SizedQueue.new 1
8
+ @mutex = Mutex.new
9
+ @threads = []
10
+ @exception = nil
11
+
12
+ increment size
13
+
14
+ puts @threads.inspect
15
+ end
16
+
17
+ def increment num=1
18
+ num.times do
19
+ @mutex.synchronize do
20
+ @threads.push(
21
+ Thread.new do |tid|
22
+ # puts "#{tid} Thread starting..."
23
+ loop do
24
+ begin
25
+ Thread.exit unless @running
26
+ # puts "#{tid} Wating for work..."
27
+ block = @queue.pop
28
+ # puts "#{tid} Spawn worker with #{block}"
29
+ block.call
30
+ rescue SignalException => e
31
+ puts "#{tid} Interuppted"
32
+ Thread.exit
33
+ rescue Exception => e
34
+ @exception = e
35
+ Thread.exit
36
+ puts "#{tid} Exception: #{e}"
37
+ ensure
38
+ # puts "#{tid} End worker with #{block}"
39
+ end
40
+ end
41
+ end
42
+ )
43
+ end
44
+ end
45
+ # @thread_count+=num
46
+ end
47
+
48
+ def decrement num=1
49
+ num.times do
50
+ @mutex.synchronize do
51
+ dispatch do
52
+ Thread.current.exit
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ def dispatch(&block)
59
+ @queue.push(block)
60
+ end
61
+
62
+ def join
63
+ while @queue.size > 0
64
+ raise @exception unless @exception.nil?
65
+ sleep 0.5
66
+ end
67
+
68
+ @running = false
69
+ decrement @threads.count
70
+
71
+ @threads.each do |thread|
72
+ @mutex.synchronize do
73
+ thread.join
74
+ @threads.delete thread
75
+ end
76
+ end
77
+
78
+ raise @exception unless @exception.nil?
79
+
80
+ end
81
+ end
82
+
83
+ end
@@ -0,0 +1,3 @@
1
+ module EasyDispatch
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_dispatch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ray Hilton
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-15 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Super simple concurrecy in ruby
15
+ email:
16
+ - ray@wirestorm.net
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - easy_dispatch.gemspec
27
+ - lib/core_ext.rb
28
+ - lib/core_ext/array.rb
29
+ - lib/easy_dispatch.rb
30
+ - lib/easy_dispatch/pool.rb
31
+ - lib/easy_dispatch/version.rb
32
+ homepage: ''
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.6
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Super simple concurrecy in ruby
56
+ test_files: []