active_concurrency 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.fasterer.yml +19 -0
- data/.gitignore +13 -0
- data/.irbrc +44 -0
- data/.reek.yml +42 -0
- data/.rspec +4 -0
- data/.rubocop.yml +26 -0
- data/.travis.yml +13 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +129 -0
- data/Rakefile +8 -0
- data/active_concurrency.gemspec +29 -0
- data/bin/console +15 -0
- data/bin/setup +10 -0
- data/lib/active_concurrency.rb +17 -0
- data/lib/active_concurrency/base/pool.rb +75 -0
- data/lib/active_concurrency/base/worker.rb +104 -0
- data/lib/active_concurrency/processes/pool.rb +8 -0
- data/lib/active_concurrency/processes/worker.rb +28 -0
- data/lib/active_concurrency/schedulers/least_busy.rb +19 -0
- data/lib/active_concurrency/schedulers/round_robin.rb +19 -0
- data/lib/active_concurrency/schedulers/topic.rb +38 -0
- data/lib/active_concurrency/threads/pool.rb +8 -0
- data/lib/active_concurrency/threads/worker.rb +26 -0
- data/lib/active_concurrency/version.rb +5 -0
- metadata +154 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7d2947f7f28910156b609d3008807a80adbfd10906a49151b1650a09f2817b32
|
4
|
+
data.tar.gz: 9e1b562f6472cd9e12f67636ea8522e54056ab1e1f8b27831a17d8f16a51f8e2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8fbf2aaa992ad64f5df1d78a9a3ca6ea8f724f36dbb606caa147f89002b655f7a4f83fcc4cd3b780f327dd6b74d770f6049f3ede773bafd57c44f946f571b439
|
7
|
+
data.tar.gz: a30b29230ee122645382bf1dc690d91325aec2c9dfb36ff944d09f76b9df9d5792deeb0ae8a08e2aece015e57a80ed39cfdabe9421f1e6fc02f8486bf2d99840
|
data/.fasterer.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
speedups:
|
2
|
+
rescue_vs_respond_to: true
|
3
|
+
module_eval: true
|
4
|
+
shuffle_first_vs_sample: true
|
5
|
+
for_loop_vs_each: true
|
6
|
+
each_with_index_vs_while: false
|
7
|
+
map_flatten_vs_flat_map: true
|
8
|
+
reverse_each_vs_reverse_each: true
|
9
|
+
select_first_vs_detect: true
|
10
|
+
sort_vs_sort_by: true
|
11
|
+
fetch_with_argument_vs_block: true
|
12
|
+
keys_each_vs_each_key: true
|
13
|
+
hash_merge_bang_vs_hash_brackets: true
|
14
|
+
block_vs_symbol_to_proc: true
|
15
|
+
proc_call_vs_yield: true
|
16
|
+
gsub_vs_tr: true
|
17
|
+
select_last_vs_reverse_detect: true
|
18
|
+
getter_vs_attr_reader: true
|
19
|
+
setter_vs_attr_writer: true
|
data/.gitignore
ADDED
data/.irbrc
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Awesome print
|
4
|
+
begin
|
5
|
+
require 'awesome_print'
|
6
|
+
|
7
|
+
AwesomePrint.irb!
|
8
|
+
rescue LoadError => err
|
9
|
+
warn "Couldn't load awesome_print: #{err}"
|
10
|
+
end
|
11
|
+
|
12
|
+
# IRB
|
13
|
+
require 'irb/completion'
|
14
|
+
|
15
|
+
ARGV.concat %w[--readline --prompt-mode simple]
|
16
|
+
|
17
|
+
IRB.conf[:PROMPT_MODE] = :SIMPLE
|
18
|
+
IRB.conf[:EVAL_HISTORY] = 1000
|
19
|
+
IRB.conf[:SAVE_HISTORY] = 1000
|
20
|
+
IRB.conf[:HISTORY_FILE] = File.expand_path('.irbrc_history')
|
21
|
+
|
22
|
+
# Rails
|
23
|
+
railsrc_path = File.expand_path('.irbrc_rails')
|
24
|
+
|
25
|
+
if (ENV['RAILS_ENV'] || defined?(Rails)) && File.exist?(railsrc_path)
|
26
|
+
begin
|
27
|
+
load railsrc_path
|
28
|
+
rescue Exception => err
|
29
|
+
warn "Could not load: #{railsrc_path} because of #{err}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Object
|
34
|
+
class Object
|
35
|
+
|
36
|
+
def interesting_methods
|
37
|
+
case self.class
|
38
|
+
when Class then public_methods.sort - Object.public_methods
|
39
|
+
when Module then public_methods.sort - Module.public_methods
|
40
|
+
else public_methods.sort - Object.new.public_methods
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
data/.reek.yml
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
---
|
2
|
+
detectors:
|
3
|
+
Attribute:
|
4
|
+
enabled: false
|
5
|
+
BooleanParameter:
|
6
|
+
enabled: false
|
7
|
+
ControlParameter:
|
8
|
+
enabled: false
|
9
|
+
DuplicateMethodCall:
|
10
|
+
enabled: false
|
11
|
+
FeatureEnvy:
|
12
|
+
enabled: false
|
13
|
+
InstanceVariableAssumption:
|
14
|
+
enabled: false
|
15
|
+
IrresponsibleModule:
|
16
|
+
enabled: false
|
17
|
+
LongParameterList:
|
18
|
+
enabled: false
|
19
|
+
ManualDispatch:
|
20
|
+
enabled: false
|
21
|
+
MissingSafeMethod:
|
22
|
+
enabled: false
|
23
|
+
NestedIterators:
|
24
|
+
max_allowed_nesting: 2
|
25
|
+
NilCheck:
|
26
|
+
enabled: false
|
27
|
+
RepeatedConditional:
|
28
|
+
enabled: false
|
29
|
+
TooManyStatements:
|
30
|
+
enabled: false
|
31
|
+
UncommunicativeMethodName:
|
32
|
+
enabled: false
|
33
|
+
UncommunicativeParameterName:
|
34
|
+
enabled: false
|
35
|
+
UncommunicativeVariableName:
|
36
|
+
accept:
|
37
|
+
- '/^.$/'
|
38
|
+
- '/[0-9]$/'
|
39
|
+
UnusedParameters:
|
40
|
+
enabled: false
|
41
|
+
UtilityFunction:
|
42
|
+
enabled: false
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
AllCops:
|
2
|
+
DisplayCopNames: true
|
3
|
+
DisplayStyleGuide: true
|
4
|
+
TargetRubyVersion: 2.5
|
5
|
+
Exclude:
|
6
|
+
- 'spec/**/**/*'
|
7
|
+
Layout/EmptyLinesAroundClassBody:
|
8
|
+
Enabled: false
|
9
|
+
Layout/EmptyLinesAroundModuleBody:
|
10
|
+
Enabled: false
|
11
|
+
LineLength:
|
12
|
+
Max: 100
|
13
|
+
Lint/RescueException:
|
14
|
+
Enabled: false
|
15
|
+
Metrics/ModuleLength:
|
16
|
+
Enabled: false
|
17
|
+
Style/Alias:
|
18
|
+
EnforcedStyle: prefer_alias_method
|
19
|
+
Style/Documentation:
|
20
|
+
Enabled: false
|
21
|
+
Style/ExpandPathArguments:
|
22
|
+
Enabled: false
|
23
|
+
Style/RescueModifier:
|
24
|
+
Enabled: false
|
25
|
+
Style/RescueStandardError:
|
26
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at j.gomez@drexed.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Juan Gomez
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
# ActiveConcurrency
|
2
|
+
|
3
|
+
ActiveConcurrency that makes it easy to work with thread/process
|
4
|
+
workers and pools.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'active_concurrency'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install active_concurrency
|
21
|
+
|
22
|
+
## Table of Contents
|
23
|
+
|
24
|
+
* [Usage](#usage)
|
25
|
+
* [Workers](#worker)
|
26
|
+
* [Pools](#pools)
|
27
|
+
* [Schedulers](#schedulers)
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
Before getting started it is best to understand when to use processes vs
|
32
|
+
threads workers/pools.
|
33
|
+
|
34
|
+
Use threads when you need to access an outer shared resource or looking to
|
35
|
+
run light weight executors. You may find threads harder to debug for
|
36
|
+
unexpected errors.
|
37
|
+
|
38
|
+
Use processes when you don't need to access an outer shared resource.
|
39
|
+
Processes are also more memory intensive as the load an its
|
40
|
+
own identical system, but have the added benefit of being easier to debug.
|
41
|
+
Use the following as a guide when dealing with ActiveRecord:
|
42
|
+
https://makandracards.com/makandra/556-test-concurrent-ruby-code
|
43
|
+
|
44
|
+
Play around with both until you find the best performance mix of speed and
|
45
|
+
memory usage. Very rarely will you find any benefit of running both together.
|
46
|
+
We suggest you start with threads before using processes.
|
47
|
+
|
48
|
+
## Workers
|
49
|
+
|
50
|
+
Workers are a single unit that contain its own queue to and process data in
|
51
|
+
a FIFO sequence. You can use a worker without a pool if you need to process
|
52
|
+
a small amount items.
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
worker = ActiveConcurrency::Processes::Worker.new(name: 'us-east')
|
56
|
+
|
57
|
+
worker.schedule { expensive_code }
|
58
|
+
worker.schedule { expensive_code }
|
59
|
+
...
|
60
|
+
worker.shutdown
|
61
|
+
```
|
62
|
+
|
63
|
+
## Pools
|
64
|
+
|
65
|
+
Pools are a group of workers that queue data based on a selected scheduling
|
66
|
+
algorithm. Use a pool to process a large amount of items and spread loads
|
67
|
+
between many workers.
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
pool = ActiveConcurrency::Threads::Pool.new(size: 10)
|
71
|
+
|
72
|
+
pool.schedule { expensive_code }
|
73
|
+
pool.schedule { expensive_code }
|
74
|
+
...
|
75
|
+
pool.shutdown
|
76
|
+
```
|
77
|
+
|
78
|
+
## Schedulers
|
79
|
+
|
80
|
+
There are currently three scheduling algorithms to choose that bring
|
81
|
+
flexibility to your pools.
|
82
|
+
|
83
|
+
**Least busy (default)**
|
84
|
+
The least busy algorithm will schedule the job in the worker with
|
85
|
+
the smallest sized queue.
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
ActiveConcurrency::Threads::Pool.new(
|
89
|
+
scheduler: ActiveConcurrency::Schedulers::LeastBusy
|
90
|
+
)
|
91
|
+
```
|
92
|
+
|
93
|
+
**Round robin**
|
94
|
+
The round robin algorithm will schedule the job in the next sequentially available worker queue.
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
ActiveConcurrency::Processes::Pool.new(
|
98
|
+
scheduler: ActiveConcurrency::Schedulers::RoundRobin
|
99
|
+
)
|
100
|
+
```
|
101
|
+
|
102
|
+
**Topic**
|
103
|
+
The topic algorithm will schedule the job in the selected topic worker queue.
|
104
|
+
This is similar to background programs or message buses.
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
ActiveConcurrency::Threads::Pool.new(
|
108
|
+
scheduler: ActiveConcurrency::Schedulers::Topic,
|
109
|
+
topics: %w[topic_1 topic_2 topic_3]
|
110
|
+
)
|
111
|
+
```
|
112
|
+
|
113
|
+
## Development
|
114
|
+
|
115
|
+
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.
|
116
|
+
|
117
|
+
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).
|
118
|
+
|
119
|
+
## Contributing
|
120
|
+
|
121
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/drexed/active_concurrency. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
122
|
+
|
123
|
+
## License
|
124
|
+
|
125
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
126
|
+
|
127
|
+
## Code of Conduct
|
128
|
+
|
129
|
+
Everyone interacting in the ActiveConcurrency project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/drexed/active_concurrency/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'active_concurrency/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'active_concurrency'
|
9
|
+
spec.version = ActiveConcurrency::VERSION
|
10
|
+
spec.authors = ['Juan Gomez']
|
11
|
+
spec.email = ['j.gomez@drexed.com']
|
12
|
+
|
13
|
+
spec.summary = 'Gem for developing simple concurrency programs.'
|
14
|
+
spec.description = 'Libray for build multi thread/process programs.'
|
15
|
+
spec.homepage = 'http://drexed.github.io/active_concurrency'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
spec.bindir = 'exe'
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler'
|
24
|
+
spec.add_development_dependency 'fasterer'
|
25
|
+
spec.add_development_dependency 'rake'
|
26
|
+
spec.add_development_dependency 'reek'
|
27
|
+
spec.add_development_dependency 'rspec'
|
28
|
+
spec.add_development_dependency 'rubocop'
|
29
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#!/usr/bin/env ruby
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'active_concurrency'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require 'pry'
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_concurrency/version'
|
4
|
+
|
5
|
+
%w[least_busy round_robin topic].each do |file_name|
|
6
|
+
require "active_concurrency/schedulers/#{file_name}"
|
7
|
+
end
|
8
|
+
|
9
|
+
%w[base processes threads].each do |dir_name|
|
10
|
+
%w[worker pool].each do |file_name|
|
11
|
+
require "active_concurrency/#{dir_name}/#{file_name}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module ActiveConcurrency
|
16
|
+
class Error < StandardError; end
|
17
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveConcurrency
|
4
|
+
module Base
|
5
|
+
class Pool
|
6
|
+
|
7
|
+
DEFAULT_SCHEDULER ||= ActiveConcurrency::Schedulers::LeastBusy
|
8
|
+
|
9
|
+
def initialize(size: 2, scheduler: DEFAULT_SCHEDULER, **options)
|
10
|
+
size = [size, options[:topics].size].max if options.key?(:topics)
|
11
|
+
@pool = Array.new(size) { |n| worker.new(name: n) }
|
12
|
+
@scheduler = scheduler.new(@pool, options)
|
13
|
+
end
|
14
|
+
|
15
|
+
def clear
|
16
|
+
@pool.map(&:clear)
|
17
|
+
end
|
18
|
+
|
19
|
+
def close
|
20
|
+
@pool.map(&:close)
|
21
|
+
end
|
22
|
+
|
23
|
+
def closed
|
24
|
+
@pool.each_with_object({}) do |w, h|
|
25
|
+
h[w.name] = w.closed?
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def exit
|
30
|
+
@pool.map(&:exit)
|
31
|
+
end
|
32
|
+
|
33
|
+
def exit!
|
34
|
+
@pool.map(&:exit!)
|
35
|
+
end
|
36
|
+
|
37
|
+
def schedule(*args, &block)
|
38
|
+
@scheduler.schedule(*args, &block)
|
39
|
+
end
|
40
|
+
|
41
|
+
def join
|
42
|
+
@pool.map(&:join)
|
43
|
+
end
|
44
|
+
|
45
|
+
def lock
|
46
|
+
@pool.map(&:lock)
|
47
|
+
end
|
48
|
+
|
49
|
+
def sizes
|
50
|
+
@pool.each_with_object({}) do |w, h|
|
51
|
+
h[w.name] = w.size
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def shutdown
|
56
|
+
@pool.map(&:shutdown)
|
57
|
+
end
|
58
|
+
|
59
|
+
def statuses
|
60
|
+
@pool.each_with_object({}) do |w, h|
|
61
|
+
h[w.name] = w.status
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def worker
|
68
|
+
modules = self.class.name.split('::')[0..1]
|
69
|
+
klass = modules.push('Worker').join('::')
|
70
|
+
::Object.const_get(klass)
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'securerandom'
|
4
|
+
|
5
|
+
module ActiveConcurrency
|
6
|
+
module Base
|
7
|
+
class Worker
|
8
|
+
|
9
|
+
attr_reader :name
|
10
|
+
attr_accessor :mutex
|
11
|
+
|
12
|
+
def initialize(name: nil)
|
13
|
+
@name = "#{prefix}_worker_#{name || SecureRandom.uuid}"
|
14
|
+
@queue = Queue.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def clear
|
18
|
+
@queue.clear
|
19
|
+
end
|
20
|
+
|
21
|
+
def close
|
22
|
+
@queue.close
|
23
|
+
end
|
24
|
+
|
25
|
+
def closed?
|
26
|
+
@queue.closed?
|
27
|
+
end
|
28
|
+
|
29
|
+
def empty?
|
30
|
+
@queue.empty?
|
31
|
+
end
|
32
|
+
|
33
|
+
def exit
|
34
|
+
schedule { throw :exit }
|
35
|
+
end
|
36
|
+
|
37
|
+
def lock
|
38
|
+
return true if process? || mutex.nil? || mutex.locked?
|
39
|
+
|
40
|
+
mutex.lock
|
41
|
+
end
|
42
|
+
|
43
|
+
def schedule(*args, &block)
|
44
|
+
@queue << [block, args]
|
45
|
+
end
|
46
|
+
|
47
|
+
# rubocop:disable Lint/UnreachableCode
|
48
|
+
def shutdown
|
49
|
+
exit
|
50
|
+
lock
|
51
|
+
join
|
52
|
+
close
|
53
|
+
exit!
|
54
|
+
end
|
55
|
+
# rubocop:enable Lint/UnreachableCode
|
56
|
+
|
57
|
+
def size
|
58
|
+
@queue.size
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def execute
|
64
|
+
job, args = @queue.pop
|
65
|
+
|
66
|
+
if mutex.nil? || process?
|
67
|
+
job.call(*args)
|
68
|
+
else
|
69
|
+
mutex.synchronize { job.call(*args) }
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def prefix
|
74
|
+
@prefix ||= begin
|
75
|
+
klass = self.class.name.split('::')[1]
|
76
|
+
klass.downcase
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def perform
|
81
|
+
catch(:exit) do
|
82
|
+
loop do
|
83
|
+
break if closed? || empty?
|
84
|
+
|
85
|
+
begin
|
86
|
+
execute
|
87
|
+
rescue Exception => e
|
88
|
+
puts "#{e.class.name}: #{e.message}\n#{e.backtrace.join("\n")}"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def process?
|
95
|
+
prefix == 'processes'
|
96
|
+
end
|
97
|
+
|
98
|
+
def thread?
|
99
|
+
prefix == 'threads'
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveConcurrency
|
4
|
+
module Processes
|
5
|
+
class Worker < ActiveConcurrency::Base::Worker
|
6
|
+
|
7
|
+
attr_reader :status
|
8
|
+
|
9
|
+
def initialize(name: nil)
|
10
|
+
super(name: name)
|
11
|
+
@status = 'run'
|
12
|
+
end
|
13
|
+
|
14
|
+
def exit!
|
15
|
+
Process.waitpid(@process) unless @process.nil?
|
16
|
+
@status = false
|
17
|
+
end
|
18
|
+
|
19
|
+
def join
|
20
|
+
@process = Process.fork do
|
21
|
+
perform
|
22
|
+
at_exit { exit! }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveConcurrency
|
4
|
+
module Schedulers
|
5
|
+
class LeastBusy
|
6
|
+
|
7
|
+
def initialize(pool, _options)
|
8
|
+
mutex = Mutex.new
|
9
|
+
@pool = pool.each { |w| w.mutex = mutex }
|
10
|
+
end
|
11
|
+
|
12
|
+
def schedule(*args, &block)
|
13
|
+
worker = @pool.min_by(&:size)
|
14
|
+
worker.schedule(*args, &block)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveConcurrency
|
4
|
+
module Schedulers
|
5
|
+
class RoundRobin
|
6
|
+
|
7
|
+
def initialize(pool, _options)
|
8
|
+
mutex = Mutex.new
|
9
|
+
@pool = pool.each { |w| w.mutex = mutex }.cycle
|
10
|
+
end
|
11
|
+
|
12
|
+
def schedule(*args, &block)
|
13
|
+
worker = @pool.next
|
14
|
+
worker.schedule(*args, &block)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveConcurrency
|
4
|
+
module Schedulers
|
5
|
+
class Topic
|
6
|
+
|
7
|
+
def initialize(pool, **options)
|
8
|
+
topics = options[:topics]
|
9
|
+
mutexes = topics.each_with_object({}) do |t, h|
|
10
|
+
h[t] = Mutex.new
|
11
|
+
end
|
12
|
+
|
13
|
+
topics = topics.cycle
|
14
|
+
@pool = pool.each_with_object({}) do |w, h|
|
15
|
+
topic = topics.next
|
16
|
+
w.mutex = mutexes[topic]
|
17
|
+
h.key?(topic) ? (h[topic] << w) : (h[topic] = [w])
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def schedule(*args, &block)
|
22
|
+
topic = args.pop
|
23
|
+
worker = @pool[topic].min_by(&:size)
|
24
|
+
worker.schedule(*args, &block)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def topics_pool
|
30
|
+
pool.each_with_object({}) do |w, h|
|
31
|
+
topic = topics.next
|
32
|
+
h.key?(topic) ? (h[topic] << w) : (h[topic] = [w])
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveConcurrency
|
4
|
+
module Threads
|
5
|
+
class Worker < ActiveConcurrency::Base::Worker
|
6
|
+
|
7
|
+
def initialize(name: nil)
|
8
|
+
super(name: name)
|
9
|
+
@thread = Thread.new(@name) { perform }
|
10
|
+
end
|
11
|
+
|
12
|
+
def exit!
|
13
|
+
@thread.exit
|
14
|
+
end
|
15
|
+
|
16
|
+
def join
|
17
|
+
@thread.join
|
18
|
+
end
|
19
|
+
|
20
|
+
def status
|
21
|
+
@thread.status
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_concurrency
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Juan Gomez
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-02-13 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: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: fasterer
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: reek
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Libray for build multi thread/process programs.
|
98
|
+
email:
|
99
|
+
- j.gomez@drexed.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".fasterer.yml"
|
105
|
+
- ".gitignore"
|
106
|
+
- ".irbrc"
|
107
|
+
- ".reek.yml"
|
108
|
+
- ".rspec"
|
109
|
+
- ".rubocop.yml"
|
110
|
+
- ".travis.yml"
|
111
|
+
- CODE_OF_CONDUCT.md
|
112
|
+
- Gemfile
|
113
|
+
- Gemfile.lock
|
114
|
+
- LICENSE.txt
|
115
|
+
- README.md
|
116
|
+
- Rakefile
|
117
|
+
- active_concurrency.gemspec
|
118
|
+
- bin/console
|
119
|
+
- bin/setup
|
120
|
+
- lib/active_concurrency.rb
|
121
|
+
- lib/active_concurrency/base/pool.rb
|
122
|
+
- lib/active_concurrency/base/worker.rb
|
123
|
+
- lib/active_concurrency/processes/pool.rb
|
124
|
+
- lib/active_concurrency/processes/worker.rb
|
125
|
+
- lib/active_concurrency/schedulers/least_busy.rb
|
126
|
+
- lib/active_concurrency/schedulers/round_robin.rb
|
127
|
+
- lib/active_concurrency/schedulers/topic.rb
|
128
|
+
- lib/active_concurrency/threads/pool.rb
|
129
|
+
- lib/active_concurrency/threads/worker.rb
|
130
|
+
- lib/active_concurrency/version.rb
|
131
|
+
homepage: http://drexed.github.io/active_concurrency
|
132
|
+
licenses:
|
133
|
+
- MIT
|
134
|
+
metadata: {}
|
135
|
+
post_install_message:
|
136
|
+
rdoc_options: []
|
137
|
+
require_paths:
|
138
|
+
- lib
|
139
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
|
+
requirements:
|
146
|
+
- - ">="
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '0'
|
149
|
+
requirements: []
|
150
|
+
rubygems_version: 3.0.2
|
151
|
+
signing_key:
|
152
|
+
specification_version: 4
|
153
|
+
summary: Gem for developing simple concurrency programs.
|
154
|
+
test_files: []
|