forkandreturn 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ 0.2.2 (11-02-2012)
2
+
3
+ * Using Tempfile instead of home brewed Utils.tempfile.
4
+
1
5
  0.2.1 (06-10-2008)
2
6
 
3
7
  * Fixed a bug concerning single-core machines.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.2
data/example.txt CHANGED
@@ -128,7 +128,7 @@ parameter:
128
128
 
129
129
  count = 0
130
130
 
131
- Dir.glob("*.gz").sort.concurrent_collect do |file|
131
+ Dir.glob("*.gz").sort.concurrent_collect(4) do |file|
132
132
  c = 0
133
133
 
134
134
  Zlib::GzipReader.open(file) do |io|
@@ -117,7 +117,7 @@ module ForkAndReturn
117
117
  # Example:
118
118
  #
119
119
  # [1, 2, 3, 4].collect do |object|
120
- # ForkAndReturn.fork_and_return do
120
+ # ForkAndReturn.fork_and_return_core do
121
121
  # object*2
122
122
  # end
123
123
  # end.collect do |wait|
@@ -131,7 +131,7 @@ module ForkAndReturn
131
131
  # This runs each "object*2" statement in a seperate process, concurrently.
132
132
 
133
133
  def self.fork_and_return_core(*args, &block)
134
- file = Util.tempfile
134
+ tempfile = Tempfile.new("fork_and_return").unlink
135
135
 
136
136
  #begin
137
137
  pid =
@@ -149,11 +149,9 @@ module ForkAndReturn
149
149
  ok, res = false, $!
150
150
  end
151
151
 
152
- File.open(file, "wb") do |f|
153
- f.chmod(0600)
152
+ Marshal.dump([ok, res], tempfile)
154
153
 
155
- Marshal.dump([ok, res], f)
156
- end
154
+ tempfile.close
157
155
  end
158
156
  #rescue Errno::EAGAIN # Resource temporarily unavailable - fork(2)
159
157
  # Kernel.sleep 0.1
@@ -166,19 +164,15 @@ module ForkAndReturn
166
164
 
167
165
  lambda do # Load the result and delete the temp file.
168
166
  begin
169
- if File.owned?(file)
170
- ok, res = File.open(file, "rb"){|f| Marshal.load(f.read)}
171
- else
172
- ok, res = false, WorkerError.new("you're not the owner of the temporary file")
173
- end
174
- rescue Errno::ENOENT # No such file or directory
175
- ok, res = false, WorkerError.new("the worker hasn't returned a result")
167
+ tempfile.rewind
168
+
169
+ ok, res = *Marshal.load(tempfile)
176
170
  rescue EOFError # end of file reached
177
171
  ok, res = false, WorkerError.new("the worker hasn't returned a result")
178
172
  rescue TypeError # can't be read
179
173
  ok, res = false, WorkerError.new("the worker has returned corrupt data")
180
174
  ensure
181
- File.delete(file) if File.file?(file)
175
+ tempfile.close
182
176
  end
183
177
 
184
178
  lambda do # Handle the result.
@@ -45,30 +45,5 @@ module ForkAndReturn
45
45
  false
46
46
  end
47
47
  end
48
-
49
- def self.generate_counter(count=0)
50
- fun =
51
- lambda do
52
- Thread.exclusive do
53
- count = count.succ
54
- end
55
- end
56
-
57
- class << fun
58
- alias next call
59
- end
60
-
61
- fun
62
- end
63
-
64
- @tempfile_counter = Util.generate_counter
65
-
66
- def self.tempfile
67
- File.join(tempdir, "%s.%d.%d.tmp" % ["fork_and_return", $$, @tempfile_counter.next])
68
- end
69
-
70
- def self.tempdir
71
- [ENV["TMPDIR"], ENV["TMP"], ENV["TEMP"], "/tmp", "c:/temp"].compact.find{|dir| File.directory?(dir)}
72
- end
73
48
  end
74
49
  end
@@ -0,0 +1,3 @@
1
+ module ForkAndReturn
2
+ VERSION = "0.2.2"
3
+ end
data/lib/forkandreturn.rb CHANGED
@@ -1,5 +1,8 @@
1
+ require "tempfile"
2
+
1
3
  require "threadlimiter"
2
4
 
5
+ require "forkandreturn/version"
3
6
  require "forkandreturn/forkandreturn"
4
7
  require "forkandreturn/exceptions"
5
8
  require "forkandreturn/util"
data/test/test.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "test/unit"
2
+ require "rubygems"
2
3
  require "forkandreturn"
3
4
 
4
5
  class ForkAndReturnTest < Test::Unit::TestCase
@@ -266,4 +267,8 @@ class ForkAndReturnEnumerableTest < Test::Unit::TestCase
266
267
  assert_equal(result, data.clustered_concurrent_each(3, &block))
267
268
  assert_equal(result, data.clustered_concurrent_each(-1, &block))
268
269
  end
270
+
271
+ def test_multi_core
272
+ assert_equal(true, ForkAndReturn::Util.multi_core?)
273
+ end
269
274
  end
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forkandreturn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ hash: 19
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 2
10
+ version: 0.2.2
5
11
  platform: ruby
6
12
  authors:
7
13
  - Erik Veenstra
@@ -9,19 +15,24 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2008-10-06 00:00:00 +02:00
13
- default_executable:
18
+ date: 2012-02-11 00:00:00 Z
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: threadlimiter
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
20
25
  requirements:
21
26
  - - ">="
22
27
  - !ruby/object:Gem::Version
28
+ hash: 31
29
+ segments:
30
+ - 0
31
+ - 1
32
+ - 2
23
33
  version: 0.1.2
24
- version:
34
+ type: :runtime
35
+ version_requirements: *id001
25
36
  description: Runs a block of code in a seperate process and collects the result later. Includes a lot of convenient methods on Enumerable.
26
37
  email: forkandreturn@erikveen.dds.nl
27
38
  executables: []
@@ -31,19 +42,21 @@ extensions: []
31
42
  extra_rdoc_files: []
32
43
 
33
44
  files:
34
- - lib/forkandreturn
35
- - lib/forkandreturn/exceptions.rb
36
- - lib/forkandreturn/enumerable.rb
37
45
  - lib/forkandreturn/forkandreturn.rb
38
46
  - lib/forkandreturn/util.rb
47
+ - lib/forkandreturn/enumerable.rb
48
+ - lib/forkandreturn/version.rb
49
+ - lib/forkandreturn/exceptions.rb
39
50
  - lib/forkandreturn.rb
40
51
  - README
41
52
  - LICENSE
42
53
  - VERSION
43
54
  - CHANGELOG
44
55
  - example.txt
45
- has_rdoc: true
56
+ - test/test.rb
46
57
  homepage: http://www.erikveen.dds.nl/forkandreturn/index.html
58
+ licenses: []
59
+
47
60
  post_install_message:
48
61
  rdoc_options:
49
62
  - README
@@ -52,29 +65,35 @@ rdoc_options:
52
65
  - CHANGELOG
53
66
  - example.txt
54
67
  - --title
55
- - forkandreturn (0.2.1)
68
+ - forkandreturn (0.2.2)
56
69
  - --main
57
70
  - README
58
71
  require_paths:
59
72
  - lib
60
73
  required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
61
75
  requirements:
62
76
  - - ">="
63
77
  - !ruby/object:Gem::Version
78
+ hash: 3
79
+ segments:
80
+ - 0
64
81
  version: "0"
65
- version:
66
82
  required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
67
84
  requirements:
68
85
  - - ">="
69
86
  - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
70
90
  version: "0"
71
- version:
72
91
  requirements: []
73
92
 
74
93
  rubyforge_project: forkandreturn
75
- rubygems_version: 1.2.0
94
+ rubygems_version: 1.8.12
76
95
  signing_key:
77
- specification_version: 2
96
+ specification_version: 3
78
97
  summary: Runs a block of code in a seperate process and collects the result later. Includes a lot of convenient methods on Enumerable.
79
98
  test_files:
80
99
  - test/test.rb