in_threads 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest ADDED
@@ -0,0 +1,7 @@
1
+ Manifest
2
+ README.rdoc
3
+ Rakefile
4
+ VERSION.yml
5
+ lib/in_threads.rb
6
+ spec/in_threads_spec.rb
7
+ spec/spec_helper.rb
data/README.rdoc ADDED
@@ -0,0 +1,63 @@
1
+ = progress
2
+
3
+ * http://github.com/toy/in_threads
4
+
5
+ == DESCRIPTION:
6
+
7
+ Easily execute ruby code in parallel
8
+
9
+ == SYNOPSIS:
10
+
11
+ By default there is maximum of 10 simultaneous threads
12
+
13
+ urls.in_threads.each do |url|
14
+ url.save_to_disk
15
+ end
16
+
17
+ urls.in_threads.map do |url|
18
+ url.fetch
19
+ end
20
+
21
+ numbers.in_threads(2).map do |number|
22
+
23
+ # whery long and complicated formula
24
+ # using only 2 threads
25
+ end
26
+
27
+ You can use any Enumerable method but it is up to you if this is good
28
+
29
+ urls.in_threads.any?(&:ok?)
30
+ urls.in_threads.all?(&:ok?)
31
+
32
+ == REQUIREMENTS:
33
+
34
+ * ruby )))
35
+
36
+ == INSTALL:
37
+
38
+ * sudo gem install in_threads
39
+
40
+ == LICENSE:
41
+
42
+ (The MIT License)
43
+
44
+ Copyright (c) 2008 toy
45
+
46
+ Permission is hereby granted, free of charge, to any person obtaining
47
+ a copy of this software and associated documentation files (the
48
+ 'Software'), to deal in the Software without restriction, including
49
+ without limitation the rights to use, copy, modify, merge, publish,
50
+ distribute, sublicense, and/or sell copies of the Software, and to
51
+ permit persons to whom the Software is furnished to do so, subject to
52
+ the following conditions:
53
+
54
+ The above copyright notice and this permission notice shall be
55
+ included in all copies or substantial portions of the Software.
56
+
57
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
58
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
59
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
60
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
61
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
62
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
63
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/clean'
4
+ require 'fileutils'
5
+ require 'echoe'
6
+
7
+ version = YAML.load_file(File.join(File.dirname(__FILE__), 'VERSION.yml')).join('.') rescue nil
8
+
9
+ echoe = Echoe.new('in_threads', version) do |p|
10
+ p.author = 'toy'
11
+ p.summary = 'Execute ruby code in parallel.'
12
+ p.project = 'toytoy'
13
+ end
14
+
15
+ desc "Replace system gem with symlink to this folder"
16
+ task :ghost do
17
+ path = Gem.searcher.find(echoe.name).full_gem_path
18
+ system 'sudo', 'rm', '-r', path
19
+ symlink File.expand_path('.'), path
20
+ end
data/VERSION.yml ADDED
@@ -0,0 +1 @@
1
+ [0, 0, 1]
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{in_threads}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["toy"]
9
+ s.date = %q{2009-09-21}
10
+ s.description = %q{Execute ruby code in parallel.}
11
+ s.email = %q{}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/in_threads.rb"]
13
+ s.files = ["Manifest", "README.rdoc", "Rakefile", "VERSION.yml", "lib/in_threads.rb", "spec/in_threads_spec.rb", "spec/spec_helper.rb", "in_threads.gemspec"]
14
+ s.homepage = %q{}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "In_threads", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{toytoy}
18
+ s.rubygems_version = %q{1.3.5}
19
+ s.summary = %q{Execute ruby code in parallel.}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
data/lib/in_threads.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'thread'
2
+ require 'thwait'
3
+
4
+ module Enumerable
5
+ def in_threads(max_threads = 10)
6
+ InThreads.new(self, max_threads)
7
+ end
8
+ end
9
+
10
+ class InThreads
11
+ def initialize(object, max_threads)
12
+ @threads = []
13
+ @object = object
14
+ @max_threads = max_threads
15
+ end
16
+
17
+ def map(*args, &block)
18
+ run_in_threads(:map, *args, &block)
19
+ @threads.map(&:value)
20
+ end
21
+
22
+ def method_missing(method, *args, &block)
23
+ run_in_threads(method, *args, &block)
24
+ end
25
+
26
+ private
27
+
28
+ def run_in_threads(method, *args, &block)
29
+ @object.send(method, *args) do |*args|
30
+ if @threads.count(&:alive?) >= @max_threads
31
+ ThreadsWait.new(*@threads).next_wait
32
+ end
33
+ @threads << Thread.new(*args, &block)
34
+ end
35
+ ensure
36
+ @threads.map(&:join)
37
+ end
38
+ end
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe InThreads do
4
+ before :each do
5
+ srand 1
6
+ @a = (1..30).map{ |i| mock(:"e#{i}", :hello => nil, :value => i, :rand => rand * 0.01) }
7
+ @sleepy_prock = proc{ |e| sleep(e.rand); e.hello; e.value }
8
+ @sleepy_prock_a = proc{ |e| sleep(e.inject{ |sum, e| e.rand }); e.hash }
9
+ end
10
+
11
+ def measure
12
+ start = Time.now
13
+ yield
14
+ Time.now - start
15
+ end
16
+
17
+ it "should execute block for each element" do
18
+ @a.each{ |e| e.should_receive(:hello) }
19
+ @a.in_threads.each(&:hello)
20
+ end
21
+
22
+ it "should run faster than without threads" do
23
+ (measure{ @a.in_threads.each(&@sleepy_prock) } * 2).should be < measure{ @a.each(&@sleepy_prock) }
24
+ end
25
+
26
+ it "should run not much slower than max of block running times if ran simultaneously" do
27
+ measure{ (1..95).in_threads(100).each{ |i| sleep(i * 0.01) } }.should be < 1.0
28
+ end
29
+
30
+ it "should run faster when ran with more threads" do
31
+ (measure{ @a.in_threads(20).each(&@sleepy_prock) } * 2).should be < measure{ @a.in_threads(2).each(&@sleepy_prock) }
32
+ end
33
+
34
+ %w(each map any? all? none?).each do |method|
35
+ it "should return same as without thread for #{method}" do
36
+ @a.in_threads.send(method, &@sleepy_prock).should == @a.send(method, &@sleepy_prock)
37
+ end
38
+ end
39
+
40
+ it "should return same as without thread for .each_slice" do
41
+ @a.in_threads.each_slice(2, &@sleepy_prock_a).should == @a.each_slice(2, &@sleepy_prock_a)
42
+ end
43
+ end
@@ -0,0 +1,10 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
10
+ require 'in_threads'
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: in_threads
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - toy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-21 00:00:00 +04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Execute ruby code in parallel.
17
+ email: ""
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - lib/in_threads.rb
25
+ files:
26
+ - Manifest
27
+ - README.rdoc
28
+ - Rakefile
29
+ - VERSION.yml
30
+ - lib/in_threads.rb
31
+ - spec/in_threads_spec.rb
32
+ - spec/spec_helper.rb
33
+ - in_threads.gemspec
34
+ has_rdoc: true
35
+ homepage: ""
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --line-numbers
41
+ - --inline-source
42
+ - --title
43
+ - In_threads
44
+ - --main
45
+ - README.rdoc
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "1.2"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project: toytoy
63
+ rubygems_version: 1.3.5
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Execute ruby code in parallel.
67
+ test_files: []
68
+