avertasks 0.2.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 79c837ad19ecaceaca12d8b4cf077cce9b6e9af618fba5696a928f64b6c81187
4
+ data.tar.gz: 8ef59d946cbb9a28c5bceaf37af088cdbb82ce335bb32c478e1c51f4555b87e0
5
+ SHA512:
6
+ metadata.gz: 2083e3e65f13878bad68879c7a9656df5d663a51a8e56a36cc2315fd4b00ac5319d7b034722a076e74f5ecc13719617452104d7736533ca9078cfc6cdb7fdfec
7
+ data.tar.gz: 7e5070e67779055a7f3c8d468a6d329a5dac4df7beb77ff2104a0671399543def6dc287287fb354bb2acca616f77dd6e8ebcf8888e1af4718c222f13bdd3f28c
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -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 rgbmarco@gmail.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
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in avertasks.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 MarcoEpsilon
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.
@@ -0,0 +1,60 @@
1
+ # Avertasks
2
+
3
+ It's my gem, I write it to solve my problem about use multi-threads to download files from network..etc, which have common in handling many same work or task in multi-threads
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'avertasks'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install avertasks
20
+
21
+ ## Usage
22
+ More detail you should look lib/*
23
+ ```ruby
24
+ require 'avertasks'
25
+ def fibonacci(n)
26
+ if (n == 1 || n == 2) then
27
+ return 1
28
+ else
29
+ return fibonacci(n - 1) + fibonacci(n - 2)
30
+ end
31
+ end
32
+ def main()
33
+ taskmanager = TaskManager.new
34
+ 5.upto(35).each do |index|
35
+ taskmanager.add_task(index) { puts fibonacci(index) }
36
+ end
37
+ # wait all tasks completed ...
38
+ taskmanager.join
39
+ puts "vertify tasks counts: #{TaskManager.run_times}"
40
+ end
41
+ main()
42
+ ```
43
+
44
+ ## Development
45
+
46
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
47
+
48
+ 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).
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/avertasks. 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.
53
+
54
+ ## License
55
+
56
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
57
+
58
+ ## Code of Conduct
59
+
60
+ Everyone interacting in the Avertasks project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/avertasks/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,33 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "avertasks/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "avertasks"
7
+ spec.version = Avertasks::VERSION
8
+ spec.authors = ["MarcoEpsilon"]
9
+ spec.email = ["rgbmarco@gmail.com"]
10
+
11
+ spec.summary = %q{Produce reasonable number of threads to completed the tasks}
12
+ spec.description = %q{To use it, we only need placed tasks in taskqueue and wait it, like download many files from network}
13
+ spec.homepage = "https://github.com/MarcoEpsilon/self-rubygems/tree/avertasks"
14
+ spec.license = "MIT"
15
+
16
+ #spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/MarcoEpsilon/self-rubygems/tree/avertasks"
20
+ spec.metadata["changelog_uri"] = "https://github.com/MarcoEpsilon/self-rubygems/tree/avertasks"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } + Dir.glob('./lib/avertasks/*').append(*Dir.glob('./lib/test/*').append(*Dir.glob('./lib/*')))
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 2.0"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "avertasks"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ require "avertasks/version"
2
+ require "avertasks/taskmanager"
3
+ module Avertasks
4
+ class Error < StandardError; end
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,15 @@
1
+ class Task
2
+ class TaskErr < StandardError
3
+ end
4
+ @@errMsg = [
5
+ 'you need pass run block to initialize'
6
+ ]
7
+ def initialize(*args, &block)
8
+ raise TaskErr.new(@@errMsg[0]) unless block_given?
9
+ @task_proc = block
10
+ @args = args
11
+ end
12
+ def run()
13
+ return @task_proc.call(*@args)
14
+ end
15
+ end
@@ -0,0 +1,66 @@
1
+ require File.join(File.dirname(__FILE__), 'task.rb')
2
+ require File.join(File.dirname(__FILE__), 'taskqueue.rb')
3
+ class TaskManager
4
+ @@queue = []
5
+ @@thread_lookup = Hash.new
6
+ @@queue_mutex = Mutex.new
7
+ @@should_sort = 10
8
+ @@add_count = 0
9
+ @@full_count = 1
10
+ def initialize()
11
+ end
12
+ def add_task(*args, &block)
13
+ begin
14
+ task = Task.new(*args, &block)
15
+ add_to_hungry_queue(task)
16
+ rescue => exception
17
+ raise exception
18
+ end
19
+ end
20
+ def broadcast_cankill()
21
+ @@queue_mutex.synchronize do
22
+ @@queue.each { |item| item.cankill = true }
23
+ end
24
+ end
25
+ def join()
26
+ self.broadcast_cankill()
27
+ @@queue_mutex.synchronize do
28
+ @@thread_lookup.each_value do |thr|
29
+ thr.join
30
+ end
31
+ end
32
+ end
33
+ class << self
34
+ def run_times()
35
+ return TaskQueue.run_times
36
+ end
37
+ end
38
+ private
39
+ def add_to_hungry_queue(task)
40
+ @@queue_mutex.synchronize do
41
+ @@add_count += 1
42
+ if @@add_count > @@should_sort then
43
+ @@queue.sort_by { |item| -item.hungry? }
44
+ @@add_count = 0
45
+ end
46
+ tasks = @@queue.find do |obj|
47
+ if obj.hungry? == 0 then
48
+ false
49
+ else
50
+ true
51
+ end
52
+ end
53
+ unless tasks.nil? then
54
+ tasks.push(task)
55
+ else
56
+ task_queue = TaskQueue.new(@@queue.size + 1, @@queue.size + 1).push(task)
57
+ @@queue.push(task_queue)
58
+ @@thread_lookup[task_queue.id] = task_queue.start()
59
+ @@full_count = @@queue.size
60
+ @@queue.each do |queue|
61
+ queue.full_count = @@full_count
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,85 @@
1
+ require File.join(File.dirname(__FILE__), 'task.rb')
2
+ class TaskQueue
3
+ attr_reader :tasks
4
+ attr_reader :id
5
+ attr_reader :completed
6
+ attr_reader :runing
7
+ attr_reader :waiting
8
+ attr_accessor :full_count
9
+ attr_accessor :cankill
10
+ alias :waiting? :waiting
11
+ alias :runing? :runing
12
+ class ArgumentErr < StandardError
13
+ end
14
+ class TaskRunErr < StandardError
15
+ end
16
+ @@errMsg = [
17
+ 'ArgumentErr for func:{func} argument type: {type} is requirement'
18
+ ]
19
+ @@run_times = 0
20
+ def initialize(id, full_count)
21
+ # define for collect tasks
22
+ @tasks = []
23
+ @id = id
24
+ @completed = 0
25
+ @runing = false
26
+ @waiting = true
27
+ @task_mutex = Mutex.new
28
+ @full_count = full_count
29
+ @cankill = false
30
+ @alldo = false
31
+ end
32
+ def push(task)
33
+ raise ArgumentErr.new(TaskQueue.errMsg[0].sub(/{func}/, 'TaskQueue.push').sub(/{type}/, 'Task')) unless task.instance_of? Task
34
+ @task_mutex.synchronize { @tasks << task }
35
+ self
36
+ end
37
+ # @waiting,@runing now is useless...
38
+ def run()
39
+ @task_mutex.synchronize do
40
+ if @tasks.none? then
41
+ @waiting = true
42
+ @runing = false
43
+ @alldo = true
44
+ return false
45
+ else
46
+ @waiting = false
47
+ @runing = true
48
+ @alldo = false
49
+ # temp variable for this func
50
+ @current_task = @tasks.pop
51
+ end
52
+ end
53
+ # note that i have drop out the result of your proc
54
+ @current_task.run if @current_task
55
+ @completed += 1
56
+ @@run_times += 1
57
+ return true
58
+ end
59
+ def hungry?()
60
+ count = 0
61
+ @task_mutex.synchronize do
62
+ count = full_count - @tasks.size
63
+ end
64
+ return 0 if count <= 0
65
+ return count
66
+ end
67
+ def none?
68
+ @task_mutex.synchronize do
69
+ @tasks.none?
70
+ end
71
+ end
72
+ def start()
73
+ thr = Thread.new do
74
+ while (!cankill || !@alldo) do
75
+ Thread.pass unless self.run
76
+ end
77
+ end
78
+ return thr
79
+ end
80
+ class << self
81
+ def run_times()
82
+ return @@run_times
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,3 @@
1
+ module Avertasks
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,15 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
2
+ require 'test/unit'
3
+ require 'avertasks/task.rb'
4
+ class Use < Test::Unit::TestCase
5
+ def test_use_task()
6
+ begin
7
+ task = Task.new('hello') do |arg|
8
+ arg.capitalize
9
+ end
10
+ assert(task.run() == 'Hello', 'task run test failed')
11
+ rescue => exception
12
+ assert(false, "occur exception: #{exception.message}")
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", '..', "lib")
2
+ require 'test/unit'
3
+ require 'avertasks/taskmanager.rb'
4
+ class Use < Test::Unit::TestCase
5
+ def fibonacci(n)
6
+ if n == 1 || n == 2 then
7
+ return 1
8
+ end
9
+ return fibonacci(n - 1) + fibonacci(n - 2)
10
+ end
11
+ def test_taskmanager()
12
+ taskmanager = TaskManager.new
13
+ 10.upto(22) do |index|
14
+ taskmanager.add_task(index) { |index| puts fibonacci(index) }
15
+ end
16
+ taskmanager.join()
17
+ assert(TaskQueue.run_times == 13, 'some thread loss ...')
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ # group tests
2
+ require './tc_task.rb'
3
+ require './tc_taskmanager.rb'
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: avertasks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - MarcoEpsilon
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-09-29 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
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
+ description: To use it, we only need placed tasks in taskqueue and wait it, like download
42
+ many files from network
43
+ email:
44
+ - rgbmarco@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - "./lib/avertasks.rb"
50
+ - "./lib/avertasks/task.rb"
51
+ - "./lib/avertasks/taskmanager.rb"
52
+ - "./lib/avertasks/taskqueue.rb"
53
+ - "./lib/avertasks/version.rb"
54
+ - "./lib/test/tc_task.rb"
55
+ - "./lib/test/tc_taskmanager.rb"
56
+ - "./lib/test/ts_avertask.rb"
57
+ - ".gitignore"
58
+ - CODE_OF_CONDUCT.md
59
+ - Gemfile
60
+ - LICENSE.txt
61
+ - README.md
62
+ - Rakefile
63
+ - avertasks.gemspec
64
+ - bin/console
65
+ - bin/setup
66
+ - lib/avertasks.rb
67
+ - lib/avertasks/task.rb
68
+ - lib/avertasks/taskmanager.rb
69
+ - lib/avertasks/taskqueue.rb
70
+ - lib/avertasks/version.rb
71
+ - lib/test/tc_task.rb
72
+ - lib/test/tc_taskmanager.rb
73
+ - lib/test/ts_avertask.rb
74
+ homepage: https://github.com/MarcoEpsilon/self-rubygems/tree/avertasks
75
+ licenses:
76
+ - MIT
77
+ metadata:
78
+ homepage_uri: https://github.com/MarcoEpsilon/self-rubygems/tree/avertasks
79
+ source_code_uri: https://github.com/MarcoEpsilon/self-rubygems/tree/avertasks
80
+ changelog_uri: https://github.com/MarcoEpsilon/self-rubygems/tree/avertasks
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.0.3
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Produce reasonable number of threads to completed the tasks
100
+ test_files: []