queue_to_the_future 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ doc
19
+ .yardoc
20
+ pkg
21
+
22
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Devin Christensen
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.rdoc ADDED
@@ -0,0 +1,20 @@
1
+ = queue_to_the_future
2
+
3
+ An easy way to create asynchronous execution paths in an unobtrusive way. Queue to the Future uses a managed pool of workers
4
+ to keep overhead to a minimum.
5
+
6
+ == Synopsis
7
+ f = Future(list, of, args) do |list, of, args|
8
+ sleep(1)
9
+ "done."
10
+ end
11
+
12
+ # returns immediately
13
+ puts f.inspect #=> #<QueueToTheFuture::Job:0x7ffa641c ... >
14
+
15
+ # blocks until completed
16
+ puts f #=> done.
17
+
18
+ == Copyright
19
+
20
+ Copyright (c) 2010 Devin Christensen. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "queue_to_the_future"
8
+ gem.summary = %Q{Futures for Ruby.}
9
+ gem.description = %Q{An easy way to create asynchronous execution paths in an unobtrusive way.}
10
+ gem.email = "devin@threetrieslater.com"
11
+ gem.homepage = "http://github.com/threetrieslater/queue_to_the_future"
12
+ gem.authors = ["Devin Christensen"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_development_dependency "yard", ">= 0"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ begin
39
+ require 'yard'
40
+ YARD::Rake::YardocTask.new
41
+ rescue LoadError
42
+ task :yardoc do
43
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
44
+ end
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,28 @@
1
+ require 'thread'
2
+
3
+ require 'queue_to_the_future/coordinator'
4
+ require 'queue_to_the_future/worker'
5
+ require 'queue_to_the_future/job'
6
+
7
+ module QueueToTheFuture
8
+ @@maximum_workers = 15
9
+
10
+ def self.maximum_workers
11
+ @@maximum_workers
12
+ end
13
+
14
+ def self.maximum_workers=(number)
15
+ raise StandardError.new("Bad workforce size: #{number}. Must be at least 1.") unless (number = number.to_i) >= 1
16
+ @@maximum_workers = number
17
+ end
18
+
19
+ def self.schedule(job)
20
+ Coordinator.instance.schedule(job)
21
+ end
22
+ end
23
+
24
+ module Kernel
25
+ def Future(*args, &block)
26
+ QueueToTheFuture.schedule(QueueToTheFuture::Job.new(*args, &block))
27
+ end
28
+ end
@@ -0,0 +1,46 @@
1
+ require 'singleton'
2
+
3
+ module QueueToTheFuture
4
+ class Coordinator
5
+ include Singleton
6
+
7
+ def initialize
8
+ @job_queue = []
9
+ @workforce = []
10
+ @lock = Mutex.new
11
+ end
12
+
13
+ def next_job
14
+ synchronize { @job_queue.shift }
15
+ end
16
+
17
+ def job_count
18
+ synchronize { @job_queue.size }
19
+ end
20
+
21
+ def workforce_size
22
+ synchronize { @workforce.size }
23
+ end
24
+
25
+ def relieve(worker)
26
+ synchronize { @workforce -= [worker] }
27
+ end
28
+
29
+ def schedule(job)
30
+ synchronize do
31
+ @job_queue.push(job)
32
+
33
+ if @workforce.size < QueueToTheFuture.maximum_workers && @workforce.size < @job_queue.size
34
+ @workforce.push Worker.new(@workforce.size)
35
+ end
36
+ end
37
+
38
+ job
39
+ end
40
+
41
+ private
42
+ def synchronize(&block)
43
+ @lock.synchronize(&block)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,19 @@
1
+ module QueueToTheFuture
2
+ class Job
3
+ (instance_methods - %w[__send__ __id__ object_id inspect]).each { |meth| undef_method(meth) }
4
+
5
+ def initialize(*args, &block)
6
+ @args = args
7
+ @block = block
8
+ end
9
+
10
+ def __execute__
11
+ @result = @block.call(*@args)
12
+ end
13
+
14
+ def method_missing(*args, &block)
15
+ Thread.pass while !defined?(@result)
16
+ @result.send(*args, &block)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ module QueueToTheFuture
2
+ class Worker
3
+ def initialize(index)
4
+ @index = index
5
+ @coordinator = Coordinator.instance
6
+ dispatch
7
+ end
8
+
9
+ def dispatch
10
+ Thread.new(@index) do |index|
11
+ while index < QueueToTheFuture.maximum_workers && (work = @coordinator.next_job)
12
+ work.__execute__
13
+ Thread.pass()
14
+ end
15
+
16
+ @coordinator.relieve(self)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,18 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "QueueToTheFuture" do
4
+ it "should work" do
5
+ start = Time.now.to_f
6
+
7
+ f = Future(1, 2, 3) do |*args|
8
+ sleep(0.1); args
9
+ end
10
+
11
+ QueueToTheFuture::Coordinator.instance.workforce_size.should be(1)
12
+ f.inspect.should match(/^#<QueueToTheFuture::Job/)
13
+ f.should eql([1,2,3])
14
+ (Time.now.to_f - start).should be_close(0.1, 0.001)
15
+ Thread.pass
16
+ QueueToTheFuture::Coordinator.instance.workforce_size.should be(0)
17
+ end
18
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'queue_to_the_future'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: queue_to_the_future
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Devin Christensen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-12 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: yard
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: An easy way to create asynchronous execution paths in an unobtrusive way.
36
+ email: devin@threetrieslater.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/queue_to_the_future.rb
52
+ - lib/queue_to_the_future/coordinator.rb
53
+ - lib/queue_to_the_future/job.rb
54
+ - lib/queue_to_the_future/worker.rb
55
+ - spec/queue_to_the_future_spec.rb
56
+ - spec/spec.opts
57
+ - spec/spec_helper.rb
58
+ has_rdoc: true
59
+ homepage: http://github.com/threetrieslater/queue_to_the_future
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --charset=UTF-8
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ version:
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.3.5
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Futures for Ruby.
86
+ test_files:
87
+ - spec/queue_to_the_future_spec.rb
88
+ - spec/spec_helper.rb