async-proxy 0.0.0 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +8 -0
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/async-proxy.gemspec +5 -2
- data/lib/async-proxy.rb +1 -1
- data/lib/computed_proxy.rb +13 -3
- data/lib/object_extensions.rb +2 -2
- data/lib/object_proxy.rb +1 -1
- data/spec/async-proxy_spec.rb +5 -0
- data/spec/spec_helper.rb +1 -0
- metadata +15 -3
data/README.rdoc
CHANGED
@@ -62,6 +62,14 @@ This means that in most cases you can treat an async object exactly in the same
|
|
62
62
|
wrapped object. You generally don't need to make explicit +sync+ calls (although you can if
|
63
63
|
you want to control the exact moment the object will be "realized")
|
64
64
|
|
65
|
+
=Soft timeouts
|
66
|
+
Sometimes you are interested in a value only if it can be computed in a certain time.
|
67
|
+
|
68
|
+
async_result = async_proxy.slow_method
|
69
|
+
..
|
70
|
+
result = async_result.sync(:soft_timeout => 0.30)
|
71
|
+
|
72
|
+
|
65
73
|
= Locking and thread safety
|
66
74
|
|
67
75
|
Async Proxy makes no attempt to guarantee thread safety. It can be considered as just syntactic
|
data/Rakefile
CHANGED
@@ -11,6 +11,7 @@ begin
|
|
11
11
|
gem.homepage = "http://github.com/angelf/async-proxy"
|
12
12
|
gem.authors = ["Angel Faus"]
|
13
13
|
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
gem.add_dependency "system_timer"
|
14
15
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
16
|
end
|
16
17
|
Jeweler::GemcutterTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.1.0
|
data/async-proxy.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{async-proxy}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Angel Faus"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-09-12}
|
13
13
|
s.description = %q{turn any object into an async version of itself, so that all method calls will run asynchronously, including dependent computations}
|
14
14
|
s.email = %q{angel@vlex.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -49,11 +49,14 @@ Gem::Specification.new do |s|
|
|
49
49
|
|
50
50
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
51
51
|
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
52
|
+
s.add_runtime_dependency(%q<system_timer>, [">= 0"])
|
52
53
|
else
|
53
54
|
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
55
|
+
s.add_dependency(%q<system_timer>, [">= 0"])
|
54
56
|
end
|
55
57
|
else
|
56
58
|
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
59
|
+
s.add_dependency(%q<system_timer>, [">= 0"])
|
57
60
|
end
|
58
61
|
end
|
59
62
|
|
data/lib/async-proxy.rb
CHANGED
data/lib/computed_proxy.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
|
1
2
|
module AsyncProxy
|
2
3
|
|
3
4
|
# a specialization of ObjectProxy to be used when the wraped value is dependent on another
|
@@ -26,8 +27,17 @@ module AsyncProxy
|
|
26
27
|
self
|
27
28
|
end
|
28
29
|
|
29
|
-
|
30
|
-
|
30
|
+
# waits for the computation to finish and returns the actual result
|
31
|
+
#
|
32
|
+
# optional arguments:
|
33
|
+
# - timeout: the maximum number of seconds that the computation can take.
|
34
|
+
|
35
|
+
def sync(options = {})
|
36
|
+
if options[:timeout]
|
37
|
+
SystemTimer.timeout_after(options[:timeout]){wait_for_computation}
|
38
|
+
else
|
39
|
+
wait_for_computation
|
40
|
+
end
|
31
41
|
@result
|
32
42
|
end
|
33
43
|
|
@@ -41,7 +51,7 @@ module AsyncProxy
|
|
41
51
|
|
42
52
|
def wait_for_computation
|
43
53
|
callable.sync # ensures the callable has finished and run its callbacks
|
44
|
-
@thread.join
|
54
|
+
@thread.join unless @done
|
45
55
|
end
|
46
56
|
|
47
57
|
private
|
data/lib/object_extensions.rb
CHANGED
@@ -5,7 +5,7 @@ class Object
|
|
5
5
|
end
|
6
6
|
|
7
7
|
# ensures we are dealing with the "real" synchronous object
|
8
|
-
def sync
|
9
|
-
self # a no-op in Object, will be
|
8
|
+
def sync(options = {})
|
9
|
+
self # a no-op in Object, will be redefined in async proxies classes
|
10
10
|
end
|
11
11
|
end
|
data/lib/object_proxy.rb
CHANGED
data/spec/async-proxy_spec.rb
CHANGED
@@ -5,6 +5,7 @@ class Integer
|
|
5
5
|
Kernel.sleep(1)
|
6
6
|
self + 1
|
7
7
|
end
|
8
|
+
|
8
9
|
end
|
9
10
|
|
10
11
|
describe "AsyncProxy" do
|
@@ -48,4 +49,8 @@ describe "AsyncProxy" do
|
|
48
49
|
intermediate_plus_2.sync.should == 3
|
49
50
|
end
|
50
51
|
|
52
|
+
it "timeout" do
|
53
|
+
lambda {1.async.slow_inc.sync(:timeout => 0.5)}.should raise_error(Timeout::Error)
|
54
|
+
end
|
55
|
+
|
51
56
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
+
- 1
|
7
8
|
- 0
|
8
|
-
|
9
|
-
version: 0.0.0
|
9
|
+
version: 0.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Angel Faus
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-09-12 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -31,6 +31,18 @@ dependencies:
|
|
31
31
|
version: 1.2.9
|
32
32
|
type: :development
|
33
33
|
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: system_timer
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
version: "0"
|
44
|
+
type: :runtime
|
45
|
+
version_requirements: *id002
|
34
46
|
description: turn any object into an async version of itself, so that all method calls will run asynchronously, including dependent computations
|
35
47
|
email: angel@vlex.com
|
36
48
|
executables: []
|