worker 0.0.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +20 -31
- data/examples/adder_memo.rb +11 -0
- data/examples/simple.rb +12 -0
- data/lib/worker.rb +38 -4
- data/worker.gemspec +1 -2
- metadata +3 -2
- data/lib/worker/version.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5ffb171d04a5691eb61e7156c19bb17cc71a179af7979d60fbea10bc74ae2cd9
|
4
|
+
data.tar.gz: 2df8110bd23aa3d97c284c38a3a24fa305b712e3d55dd6e3af8e0e360f91ddc9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 00f0dbf42c4495d8878e67f39618b99496e17f09a650f461eb05f3f8535d616d41941f96a071909c341c639aa8f324ce475e220b4c5697f924733bf37dbfaa81
|
7
|
+
data.tar.gz: a4e68891bacbb67a866db310980a649a9295ade5872fef95a82df9579ac80fe3ac3be67531794660ab2cb8a37262ffea6ff21de68e35ff22ac2627f373b1ed48
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,39 +1,28 @@
|
|
1
1
|
# Worker
|
2
2
|
|
3
|
-
|
3
|
+
Thread safe inter-process synchronous workers using Ruby Queue
|
4
4
|
|
5
|
-
|
5
|
+
adder = Worker.new do |a,b|
|
6
|
+
a + b
|
7
|
+
end
|
6
8
|
|
7
|
-
|
9
|
+
multiplier = Worker.new do |a,b|
|
10
|
+
a * b
|
11
|
+
end
|
8
12
|
|
9
|
-
|
13
|
+
puts adder.perform 1, 2
|
14
|
+
# => 3
|
15
|
+
puts multiplier.perform 10, 2
|
16
|
+
# => 20
|
10
17
|
|
11
|
-
|
12
|
-
gem 'worker'
|
13
|
-
```
|
18
|
+
Scoped instance variables:
|
14
19
|
|
15
|
-
|
20
|
+
adder_memo = Worker.new do |a,b|
|
21
|
+
@sum ||= 0
|
22
|
+
@sum += a + b
|
23
|
+
end
|
16
24
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
$ gem install worker
|
22
|
-
|
23
|
-
## Usage
|
24
|
-
|
25
|
-
TODO: Write usage instructions here
|
26
|
-
|
27
|
-
## Development
|
28
|
-
|
29
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
-
|
31
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
-
|
33
|
-
## Contributing
|
34
|
-
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/worker.
|
36
|
-
|
37
|
-
## License
|
38
|
-
|
39
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
25
|
+
puts adder_memo.perform 1, 2
|
26
|
+
# => 3
|
27
|
+
puts adder_memo.perform 1, 2
|
28
|
+
# => 6
|
data/examples/simple.rb
ADDED
data/lib/worker.rb
CHANGED
@@ -1,6 +1,40 @@
|
|
1
|
-
|
1
|
+
class Worker
|
2
|
+
class Ctx
|
3
|
+
end
|
2
4
|
|
3
|
-
|
4
|
-
|
5
|
-
|
5
|
+
def initialize(&block)
|
6
|
+
@in = Queue.new
|
7
|
+
@out = Queue.new
|
8
|
+
@block = block
|
9
|
+
@ctx = Worker::Ctx.new
|
10
|
+
|
11
|
+
run!
|
12
|
+
end
|
13
|
+
|
14
|
+
def perform(*args)
|
15
|
+
@in.push args
|
16
|
+
ret = @out.pop
|
17
|
+
if ret.is_a? Exception
|
18
|
+
raise ret
|
19
|
+
else
|
20
|
+
ret
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def run!
|
25
|
+
@thread = Thread.new do
|
26
|
+
loop do
|
27
|
+
ret = @ctx.instance_exec @in.pop, &@block
|
28
|
+
@out.push ret
|
29
|
+
rescue Exception => ex
|
30
|
+
@out.push ex
|
31
|
+
end
|
32
|
+
end
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
def stop!
|
37
|
+
@thread&.kill
|
38
|
+
self
|
39
|
+
end
|
6
40
|
end
|
data/worker.gemspec
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
|
2
2
|
lib = File.expand_path("../lib", __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require "worker/version"
|
5
4
|
|
6
5
|
Gem::Specification.new do |spec|
|
7
6
|
spec.name = "worker"
|
8
|
-
spec.version =
|
7
|
+
spec.version = "0.2.0"
|
9
8
|
spec.authors = ["Matti Paksula"]
|
10
9
|
spec.email = ["matti.paksula@iki.fi"]
|
11
10
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: worker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matti Paksula
|
@@ -71,8 +71,9 @@ files:
|
|
71
71
|
- Rakefile
|
72
72
|
- bin/console
|
73
73
|
- bin/setup
|
74
|
+
- examples/adder_memo.rb
|
75
|
+
- examples/simple.rb
|
74
76
|
- lib/worker.rb
|
75
|
-
- lib/worker/version.rb
|
76
77
|
- worker.gemspec
|
77
78
|
homepage: https://github.com/matti/worker
|
78
79
|
licenses:
|
data/lib/worker/version.rb
DELETED