async_io 0.0.4 → 0.0.5
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/lib/async_io.rb +4 -4
- data/lib/async_io/base.rb +14 -18
- data/lib/async_io/version.rb +1 -1
- data/lib/async_io/worker.rb +10 -11
- data/spec/async_io/base_spec.rb +7 -10
- data/spec/async_io/worker_spec.rb +6 -18
- 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: 589bbfd6ddea54eacdcfb7d11444a98c69ebb135
|
4
|
+
data.tar.gz: 420bbac2e0bebdfd57aa1b18d90194cdcabd2b15
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6abe1c74a64b278ae7a0752177099f51863a25bd2e09890c54332e69259c2eed1ea3b269ecc1707c27054784ff160d4b81995fdb480919d94284daa9a5a3c210
|
7
|
+
data.tar.gz: ccc8233103beacdace8692b3eb379f7dca07b3f434b9a21d57aa3f132a47a2370c65cdaea7f5cafacad2fbd16f18d9744018132e405f99707cb23c34f666520a
|
data/lib/async_io.rb
CHANGED
@@ -26,12 +26,12 @@ module AsyncIO
|
|
26
26
|
# how it is available within a block later on.
|
27
27
|
# NOTE: You must pass a job ( i.e ruby block ).
|
28
28
|
#
|
29
|
-
def self.async(
|
30
|
-
async_creator.worker(payload,
|
29
|
+
def self.async(task = proc {}, &payload)
|
30
|
+
async_creator.worker(payload, task)
|
31
31
|
end
|
32
32
|
|
33
|
-
def self.async_with(
|
34
|
-
async_creator.async_with(
|
33
|
+
def self.async_with(task)
|
34
|
+
async_creator.async_with(task)
|
35
35
|
end
|
36
36
|
|
37
37
|
end
|
data/lib/async_io/base.rb
CHANGED
@@ -42,8 +42,8 @@ module AsyncIO
|
|
42
42
|
|
43
43
|
##
|
44
44
|
# It creates a new Worker, pushes it onto the queue,
|
45
|
-
# whenever a '
|
46
|
-
# it calls the payload and passes the result of
|
45
|
+
# whenever a 'task' (i.e a Ruby object ) is finished
|
46
|
+
# it calls the payload and passes the result of that task
|
47
47
|
# to it.
|
48
48
|
#
|
49
49
|
# For example:
|
@@ -54,45 +54,41 @@ module AsyncIO
|
|
54
54
|
# end
|
55
55
|
# end
|
56
56
|
#
|
57
|
-
# It returns the worker created for
|
57
|
+
# It returns the worker created for this particular task
|
58
58
|
# which you could send message +done+ to it in order
|
59
|
-
# to retrieve its
|
60
|
-
# see
|
59
|
+
# to retrieve its completed task.
|
60
|
+
# see async_io/worker.rb
|
61
61
|
#
|
62
62
|
# For example:
|
63
63
|
# result = aget_user(1) { |u| Logger.info(u.name) }
|
64
64
|
#
|
65
|
-
# #
|
65
|
+
# # task may take a while to be done...
|
66
66
|
#
|
67
67
|
# user = result.done
|
68
68
|
# user.name
|
69
69
|
# => "John"
|
70
70
|
#
|
71
|
-
# NOTE: Whenever you use the snippet above, if the
|
71
|
+
# NOTE: Whenever you use the snippet above, if the task
|
72
72
|
# has not been finished yet you will get +false+
|
73
|
-
# whenever you send a message +
|
74
|
-
#
|
73
|
+
# whenever you send a message +task+ to it. Once
|
74
|
+
# task is finished you will be able to get its result.
|
75
75
|
#
|
76
|
-
def worker(payload,
|
77
|
-
|
78
|
-
Worker.new(payload, job).tap { |w|
|
79
|
-
queue.push(w)
|
80
|
-
}
|
81
|
-
end
|
76
|
+
def worker(payload, task)
|
77
|
+
Worker.new(payload, task).tap { |w| queue.push(w) }
|
82
78
|
end
|
83
79
|
|
84
80
|
##
|
85
81
|
# Perform any sort of task that needs to be
|
86
82
|
# asynchronously done.
|
87
83
|
# NOTE: It does not return anything, as it receives
|
88
|
-
# and empty
|
84
|
+
# and empty task. ( i.e empty block of code )
|
89
85
|
#
|
90
86
|
def async(&payload)
|
91
87
|
worker(payload, proc {})
|
92
88
|
end
|
93
89
|
|
94
|
-
def async_with(
|
95
|
-
worker(proc {},
|
90
|
+
def async_with(task)
|
91
|
+
worker(proc {}, task)
|
96
92
|
end
|
97
93
|
|
98
94
|
##
|
data/lib/async_io/version.rb
CHANGED
data/lib/async_io/worker.rb
CHANGED
@@ -6,20 +6,20 @@ module AsyncIO
|
|
6
6
|
class Worker
|
7
7
|
include AsyncIO::Rescuer
|
8
8
|
|
9
|
-
attr_reader :payload, :
|
9
|
+
attr_reader :payload, :task
|
10
10
|
attr_reader :finished, :done
|
11
11
|
|
12
|
-
def initialize(payload,
|
12
|
+
def initialize(payload, task)
|
13
13
|
@payload = payload
|
14
|
-
@
|
14
|
+
@task = task
|
15
15
|
@done = false
|
16
16
|
@finished = false
|
17
17
|
end
|
18
18
|
|
19
19
|
##
|
20
20
|
# It sends payload a message +call+
|
21
|
-
# and passes the result of
|
22
|
-
#
|
21
|
+
# and passes the result of task, by sending
|
22
|
+
# task a message +call+ as well, as its argument.
|
23
23
|
# This allows us to do:
|
24
24
|
#
|
25
25
|
# aget_user(1) { |u| print u.name }
|
@@ -35,7 +35,7 @@ module AsyncIO
|
|
35
35
|
# payload = Object.new
|
36
36
|
# def payload.call(result); warn(result); end
|
37
37
|
#
|
38
|
-
#
|
38
|
+
# task is pre-definied inside a method, it can
|
39
39
|
# be anything, for example:
|
40
40
|
# worker(payload) do
|
41
41
|
# User.find(uid)
|
@@ -43,14 +43,13 @@ module AsyncIO
|
|
43
43
|
#
|
44
44
|
def call
|
45
45
|
try do
|
46
|
-
@
|
47
|
-
|
48
|
-
payload.call(done)
|
46
|
+
@done = task.call
|
47
|
+
payload.call(done).tap { @finished = true }
|
49
48
|
end
|
50
49
|
end
|
51
50
|
|
52
51
|
##
|
53
|
-
# Tries to get the first
|
52
|
+
# Tries to get the first task done, when an exception
|
54
53
|
# is raised it then calls payload again passing a
|
55
54
|
# fallback as its argument.
|
56
55
|
#
|
@@ -70,7 +69,7 @@ module AsyncIO
|
|
70
69
|
# from being raised.
|
71
70
|
# It returns an instance of OpenStruct object with the
|
72
71
|
# exception rescued ( i.e notice ) and the worker that
|
73
|
-
# was assigned to this particular
|
72
|
+
# was assigned to this particular task.
|
74
73
|
#
|
75
74
|
def fallback(notice)
|
76
75
|
OpenStruct.new({ notice: notice, worker: self })
|
data/spec/async_io/base_spec.rb
CHANGED
@@ -71,19 +71,16 @@ describe AsyncIO::Base do
|
|
71
71
|
# closures while using blocks.
|
72
72
|
# The following might be helpul:
|
73
73
|
# robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas
|
74
|
-
|
74
|
+
#
|
75
|
+
#
|
76
|
+
# See the link below for a better understading how to
|
77
|
+
# pass/call blocks of code in Ruby.
|
78
|
+
# pragdave.pragprog.com/pragdave/2005/11/symbolto_proc.html
|
75
79
|
#
|
76
80
|
it "should pass this payload block onto Worker" do
|
77
|
-
payload = -> { :
|
78
|
-
|
81
|
+
payload = -> { :im_a_payload }
|
79
82
|
worker = double
|
80
83
|
alien.stub(:worker).and_return(worker)
|
81
|
-
|
82
|
-
##
|
83
|
-
# See the link below for a better understading how to
|
84
|
-
# pass/call blocks of code in Ruby.
|
85
|
-
# pragdave.pragprog.com/pragdave/2005/11/symbolto_proc.html
|
86
|
-
#
|
87
84
|
alien.async(&payload)
|
88
85
|
|
89
86
|
alien.should have_received(:worker).with( payload, kind_of(Proc) )
|
@@ -91,7 +88,7 @@ describe AsyncIO::Base do
|
|
91
88
|
end
|
92
89
|
|
93
90
|
context "#interval" do
|
94
|
-
it "executes a
|
91
|
+
it "executes a task within a given interval" do
|
95
92
|
alien.should_receive(:new_interval?)
|
96
93
|
alien.interval(0.0001) { sleep(10) }
|
97
94
|
end
|
@@ -9,33 +9,21 @@ module AsyncIO
|
|
9
9
|
describe Worker do
|
10
10
|
|
11
11
|
let(:payload) { ->(r) { r } }
|
12
|
-
let(:
|
13
|
-
let(:worker) { Worker.new(payload,
|
14
|
-
|
15
|
-
context "initialising" do
|
16
|
-
|
17
|
-
it "must take a payload" do
|
18
|
-
worker.payload.should be_kind_of Proc
|
19
|
-
end
|
20
|
-
|
21
|
-
it "must take a job" do
|
22
|
-
worker.job.should be_kind_of Proc
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
12
|
+
let(:task) { -> { :am_a_dog } }
|
13
|
+
let(:worker) { Worker.new(payload, task) }
|
26
14
|
|
27
15
|
it { worker.should respond_to :done }
|
28
16
|
|
29
|
-
it "should not be done as
|
17
|
+
it "should not be done as task is not finished" do
|
30
18
|
worker.done.should be_false
|
31
19
|
end
|
32
20
|
|
33
|
-
it "must call payload and pass
|
21
|
+
it "must call payload and pass task as its argument" do
|
34
22
|
worker.call.should eq(:am_a_dog)
|
35
23
|
end
|
36
24
|
|
37
|
-
context "Getting a
|
38
|
-
it "should set done to its last finished
|
25
|
+
context "Getting a task done" do
|
26
|
+
it "should set done to its last finished task" do
|
39
27
|
worker.call
|
40
28
|
worker.done.should eq(:am_a_dog)
|
41
29
|
end
|