async_rb 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 +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +51 -0
- data/Rakefile +10 -0
- data/async_rb.gemspec +23 -0
- data/lib/async.rb +3 -0
- data/lib/async/base.rb +30 -0
- data/lib/async/runner.rb +71 -0
- data/lib/async/version.rb +3 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b1d74216abda32170fe7182139e4e7b0aa6bf7ff
|
4
|
+
data.tar.gz: ef5e8bfd5dc9ebf2fb50d3c827dffff87ad588f4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cdf589e953827608212898e4b96fcbfe111755e474b0887d8f63918519bb980c4c7769695b7a073ac020bad72b2b610d2a7c55a591cd0a42705512ccea9857b4
|
7
|
+
data.tar.gz: 920d6d74bb964ca8d953bbd9179ae1c0db72c9155f91b6832a5e13ea74156a292d2228c9f4cc65871da72df91b3a5f42fa0aa6bf683670156e2ec008ce3fab9a
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Async
|
2
|
+
|
3
|
+
library of simple asynchronous utilities for ruby
|
4
|
+
|
5
|
+
## Description
|
6
|
+
|
7
|
+
I've found myself using the same patterns fo adding lightweight concurrency and parallelism in my work.
|
8
|
+
This gem is a port of those strategies to both centralize them and share them with others.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'async_rb'
|
14
|
+
```
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
```rb
|
19
|
+
#####
|
20
|
+
# Async::Base - Add easy asynchronous processing of methods similar to Go or Celluloid
|
21
|
+
#####
|
22
|
+
class MyClass
|
23
|
+
include Async::Base
|
24
|
+
|
25
|
+
def say_hello
|
26
|
+
puts "hello"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
obj = MyClass.new
|
31
|
+
|
32
|
+
# Run #say_hello synchronously
|
33
|
+
obj.say_hello
|
34
|
+
=> nil
|
35
|
+
|
36
|
+
# Run #say_hello asynchronously
|
37
|
+
obj.async(&:say_hello)
|
38
|
+
=> #<Thread:0x007fea3c8f5858 run>
|
39
|
+
|
40
|
+
|
41
|
+
#####
|
42
|
+
# Async::Runner - Easily switch between Threads, Forks, and Synchronous processing with a common interface
|
43
|
+
#####
|
44
|
+
# Strategies include :thread, :fork, and :synchronous
|
45
|
+
# Defaults to :thread
|
46
|
+
runner = Async::Runner.new(:fork)
|
47
|
+
[
|
48
|
+
runner.run { `rake db:migrate` },
|
49
|
+
runner.run { `echo hello world` }
|
50
|
+
].each(&:join)
|
51
|
+
```
|
data/Rakefile
ADDED
data/async_rb.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'async/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "async_rb"
|
8
|
+
spec.version = Async::VERSION
|
9
|
+
spec.authors = ["Josh Bodah"]
|
10
|
+
spec.email = ["jb3689@yahoo.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{library of simple asynchronous utilities for ruby}
|
13
|
+
spec.homepage = "https://github.com/jbodah/async_rb"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "minitest"
|
23
|
+
end
|
data/lib/async.rb
ADDED
data/lib/async/base.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
module Async
|
2
|
+
# A simple mixin that allows you to asynchronously process methods
|
3
|
+
# in a class similar to Go routines
|
4
|
+
module Base
|
5
|
+
# @example
|
6
|
+
#
|
7
|
+
# class MyClass
|
8
|
+
# include Async::Base
|
9
|
+
#
|
10
|
+
# def say_hello
|
11
|
+
# puts "hello"
|
12
|
+
# end
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# obj = MyClass.new
|
16
|
+
#
|
17
|
+
# # Run #say_hello synchronously
|
18
|
+
# obj.say_hello
|
19
|
+
# => nil
|
20
|
+
#
|
21
|
+
# # Run #say_hello asynchronously
|
22
|
+
# obj.async(&:say_hello)
|
23
|
+
# => #<Thread:0x007fea3c8f5858 run>
|
24
|
+
#
|
25
|
+
# @param [Block] evaluated in the context of this object
|
26
|
+
def async
|
27
|
+
Thread.new { instance_eval &Proc.new }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/async/runner.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
module Async
|
2
|
+
# Provides a common interface for performing asynchronous actions via a number of strategies
|
3
|
+
#
|
4
|
+
# @example
|
5
|
+
#
|
6
|
+
# runner = Async::Runner.new
|
7
|
+
# [
|
8
|
+
# runner.run { `rake db:migrate` },
|
9
|
+
# runner.run { `echo hello world` }
|
10
|
+
# ].each(&:join)
|
11
|
+
class Runnner
|
12
|
+
# @param [Symbol] :thread, :fork, or :synchronous
|
13
|
+
def initialize(strategy = :thread)
|
14
|
+
@strategy = Strategy.create(strategy)
|
15
|
+
end
|
16
|
+
|
17
|
+
def run
|
18
|
+
@strategy.spawn &Proc.new
|
19
|
+
end
|
20
|
+
|
21
|
+
module Strategy
|
22
|
+
def self.create(sym)
|
23
|
+
case
|
24
|
+
when :thread then Strategy::Thread
|
25
|
+
when :fork then Strategy::Fork
|
26
|
+
when :synchronous then Strategy::Synchronous
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class Base
|
31
|
+
def self.spawn(block)
|
32
|
+
instance = new(block)
|
33
|
+
instance.run!
|
34
|
+
instance
|
35
|
+
end
|
36
|
+
|
37
|
+
def initialize(block)
|
38
|
+
@block = block
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class Thread < Base
|
43
|
+
def run!
|
44
|
+
@thread = Thread.new(@block)
|
45
|
+
end
|
46
|
+
|
47
|
+
def join
|
48
|
+
@thread.join
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class Fork < Base
|
53
|
+
def run!
|
54
|
+
@pid = fork(@block)
|
55
|
+
end
|
56
|
+
|
57
|
+
def join
|
58
|
+
Process.wait @pid
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class Synchronous < Base
|
63
|
+
def run!
|
64
|
+
@block.call
|
65
|
+
end
|
66
|
+
|
67
|
+
def join; end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: async_rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Josh Bodah
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- jb3689@yahoo.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- async_rb.gemspec
|
68
|
+
- lib/async.rb
|
69
|
+
- lib/async/base.rb
|
70
|
+
- lib/async/runner.rb
|
71
|
+
- lib/async/version.rb
|
72
|
+
homepage: https://github.com/jbodah/async_rb
|
73
|
+
licenses: []
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.4.8
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: library of simple asynchronous utilities for ruby
|
95
|
+
test_files: []
|