futuroscope 0.0.12 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +0 -4
- data/README.md +2 -2
- data/lib/futuroscope/future.rb +10 -10
- data/lib/futuroscope/pool.rb +4 -5
- data/lib/futuroscope/version.rb +1 -1
- data/spec/futuroscope/pool_spec.rb +7 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2e406444d572ba1879ab2fb7963b81d27710dc5
|
4
|
+
data.tar.gz: dc9f7d86d65224ae8b48da33930a9439c814c272
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c13e918f2e3fbefd5ba7af31febc58efe08324ee4e75d430a558fcf90b936621a83faded2af549945c0d8e83d8febc83c6480290b6f2770754156dc73ca31b5
|
7
|
+
data.tar.gz: 327fdd0b92c35ac66b332af2b8ce7e7d25c13d01810213228b676dd10313480b38e072931a1c07c31be5260c0b476ff799f30b989daa25bdaa9e4e258858c511
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -23,7 +23,7 @@ method on in it will be forwarded to the block's return value.
|
|
23
23
|
If the thread didn't finish yet, it will block the program's execution until
|
24
24
|
it's finished. Otherwise, it will immediataly return its value.
|
25
25
|
|
26
|
-
Futuroscope is tested on `MRI 1.9.3`, `MRI 2.0.0`, `
|
26
|
+
Futuroscope is tested on `MRI 1.9.3`, `MRI 2.0.0`, `Rubinius (1.9)` and `JRuby (1.9)`.
|
27
27
|
|
28
28
|
## Installation
|
29
29
|
|
@@ -148,7 +148,7 @@ Futuroscope.default_pool.max_workers = 16
|
|
148
148
|
Also, each future can be scheduled to a different pool like this:
|
149
149
|
|
150
150
|
```Ruby
|
151
|
-
pool = Futuroscope::Pool.new(16
|
151
|
+
pool = Futuroscope::Pool.new(16..32)
|
152
152
|
|
153
153
|
future = Future.new(pool){ :edballs }
|
154
154
|
|
data/lib/futuroscope/future.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
|
+
require 'thread'
|
2
|
+
|
1
3
|
module Futuroscope
|
2
4
|
# A Future is an object that gets initialized with a block and will behave
|
3
5
|
# exactly like the block's result, but being able to "borrow" its result from
|
4
6
|
# the future. That is, will block when the result is not ready until it is,
|
5
7
|
# and will return it instantly if the thread's execution already finished.
|
6
8
|
#
|
7
|
-
class Future
|
8
|
-
extend Forwardable
|
9
|
+
class Future < BasicObject
|
10
|
+
extend ::Forwardable
|
9
11
|
|
10
12
|
# Initializes a future with a block and starts its execution.
|
11
13
|
#
|
@@ -22,8 +24,8 @@ module Futuroscope
|
|
22
24
|
# block - A block that will be run in the background.
|
23
25
|
#
|
24
26
|
# Returns a Future
|
25
|
-
def initialize(pool = Futuroscope.default_pool, &block)
|
26
|
-
@queue = SizedQueue.new(1)
|
27
|
+
def initialize(pool = ::Futuroscope.default_pool, &block)
|
28
|
+
@queue = ::SizedQueue.new(1)
|
27
29
|
@pool = pool
|
28
30
|
@block = block
|
29
31
|
@pool.queue self
|
@@ -31,9 +33,9 @@ module Futuroscope
|
|
31
33
|
|
32
34
|
# Semipublic: Forces this future to be run.
|
33
35
|
def run_future
|
34
|
-
@queue.push(:
|
35
|
-
rescue Exception => e
|
36
|
-
@queue.push(:
|
36
|
+
@queue.push(value: @block.call)
|
37
|
+
rescue ::Exception => e
|
38
|
+
@queue.push(exception: e)
|
37
39
|
end
|
38
40
|
|
39
41
|
# Semipublic: Returns the future's value. Will wait for the future to be
|
@@ -47,9 +49,7 @@ module Futuroscope
|
|
47
49
|
resolved[:value]
|
48
50
|
end
|
49
51
|
|
50
|
-
def_delegators :future_value,
|
51
|
-
:to_s, :==, :kind_of?, :is_a?, :clone, :class, :inspect, :tap, :to_enum,
|
52
|
-
:display, :eql?, :hash, :methods, :nil?
|
52
|
+
def_delegators :future_value, :!, :!=, :==, :equal?
|
53
53
|
|
54
54
|
private
|
55
55
|
|
data/lib/futuroscope/pool.rb
CHANGED
@@ -12,9 +12,9 @@ module Futuroscope
|
|
12
12
|
# Public: Initializes a new Pool.
|
13
13
|
#
|
14
14
|
# thread_count - The number of workers that this pool is gonna have
|
15
|
-
def initialize(
|
16
|
-
@min_workers =
|
17
|
-
@max_workers =
|
15
|
+
def initialize(range = 8..16)
|
16
|
+
@min_workers = range.min
|
17
|
+
@max_workers = range.max
|
18
18
|
@queue = Queue.new
|
19
19
|
@workers = Set.new
|
20
20
|
@mutex = Mutex.new
|
@@ -72,8 +72,7 @@ module Futuroscope
|
|
72
72
|
end
|
73
73
|
|
74
74
|
def span_chance
|
75
|
-
|
76
|
-
values.respond_to?(:sample) ? values.sample : values.choice
|
75
|
+
[true, false].sample
|
77
76
|
end
|
78
77
|
|
79
78
|
def more_workers_than_needed?
|
data/lib/futuroscope/version.rb
CHANGED
@@ -4,10 +4,10 @@ require 'futuroscope/pool'
|
|
4
4
|
module Futuroscope
|
5
5
|
describe Pool do
|
6
6
|
it "spins up a number of workers" do
|
7
|
-
pool = Pool.new(2)
|
7
|
+
pool = Pool.new(2..4)
|
8
8
|
expect(pool.workers).to have(2).workers
|
9
9
|
|
10
|
-
pool = Pool.new(3)
|
10
|
+
pool = Pool.new(3..4)
|
11
11
|
expect(pool.workers).to have(3).workers
|
12
12
|
end
|
13
13
|
|
@@ -24,7 +24,7 @@ module Futuroscope
|
|
24
24
|
|
25
25
|
describe "worker control" do
|
26
26
|
it "adds more workers when needed and returns to the default amount" do
|
27
|
-
pool = Pool.new(2
|
27
|
+
pool = Pool.new(2..8)
|
28
28
|
pool.stub(:span_chance).and_return true
|
29
29
|
10.times do |future|
|
30
30
|
Future.new(pool){ sleep(1) }
|
@@ -38,13 +38,13 @@ module Futuroscope
|
|
38
38
|
end
|
39
39
|
|
40
40
|
it "allows overriding min workers real time" do
|
41
|
-
pool = Pool.new(2
|
41
|
+
pool = Pool.new(2..8)
|
42
42
|
pool.min_workers = 3
|
43
43
|
expect(pool.workers).to have(3).workers
|
44
44
|
end
|
45
45
|
|
46
46
|
it "allows overriding max workers real time" do
|
47
|
-
pool = Pool.new(2
|
47
|
+
pool = Pool.new(2..8)
|
48
48
|
pool.stub(:span_chance).and_return true
|
49
49
|
pool.max_workers = 4
|
50
50
|
|
@@ -59,10 +59,10 @@ module Futuroscope
|
|
59
59
|
|
60
60
|
describe "#finalize" do
|
61
61
|
it "shuts down all its workers" do
|
62
|
-
pool = Pool.new(2
|
62
|
+
pool = Pool.new(2..8)
|
63
63
|
|
64
64
|
pool.send(:finalize)
|
65
|
-
|
65
|
+
|
66
66
|
expect(pool.workers).to have(0).workers
|
67
67
|
end
|
68
68
|
end
|