particle_pool 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7a65d93e8b2437683798b1c94bf740f643847d07eabb76033861500a5cfa6938
4
+ data.tar.gz: ca96ffe7be22399e02b8e0ce3d6f9f30c5a16a30943677a909528ed28d8f17f3
5
+ SHA512:
6
+ metadata.gz: ab70dbd049b061cf8eed2a3c5fe418cbdee52f513366322899a1bb7ec740e46acab33f920bc6874523c715ad49a3085bdf37cafe9a88bd3a9efe09d7ea7dd0c3
7
+ data.tar.gz: 63c4ca021036395b4a40595877559ff4fa4e7617589fd17fcc05f7b5c1bc51cb838eef0553f03407bba05c35effc3c1a9319bfa51309ab2561d0eea2821cd443
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in particle_pool.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ particle_pool (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (10.5.0)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ bundler (~> 1.16)
16
+ particle_pool!
17
+ rake (~> 10.0)
18
+
19
+ BUNDLED WITH
20
+ 1.16.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Nickolay Ilyushin
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,79 @@
1
+ # ParticlePool
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'particle_pool'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install particle_pool
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ # Require the library
23
+ require 'particle_pool'
24
+
25
+ # Create a new pool
26
+ p = ParticlePool::Pool.new
27
+
28
+ # Start it
29
+ p.start
30
+
31
+ (1024 * Etc.nprocessors).times do |i|
32
+ # Create some tasks
33
+ t = ParticlePool::Task.new do |n|
34
+ raise 'fib not defined for negative numbers' if n < 0
35
+ nv, ov = 1, 0
36
+ n.times do
37
+ nv, ov = nv + ov, nv
38
+ Fiber.yield
39
+ end
40
+ $arr << ov
41
+ $sz += 1
42
+ ov
43
+ end
44
+
45
+ # And push them into the pool.
46
+ p.push(t, i)
47
+ end
48
+
49
+ # You can wait for tasks to finish!
50
+ t = ParticlePool::Task.new do
51
+ sleep 16
52
+ 'Hello, World!'
53
+ end
54
+ p.push(t)
55
+
56
+ puts t.await
57
+
58
+ # Wait for all results to appear
59
+ while $sz != (1024 * Etc.nprocessors) do
60
+ sleep 1
61
+ end
62
+
63
+ # Sum results and print the final result
64
+ puts $arr.reduce(:+)
65
+ ```
66
+
67
+ ## Development
68
+
69
+ 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.
70
+
71
+ 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).
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/handicraftsman/particle_pool.
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'particle_pool'
5
+
6
+ require 'irb'
7
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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
data/bin/task.rb ADDED
@@ -0,0 +1,12 @@
1
+ def gettask
2
+ ParticlePool::Task.new do |n|
3
+ raise 'fib not defined for negative numbers' if n < 0
4
+ nv, ov = 1, 0
5
+ n.times do
6
+ nv, ov = nv + ov, nv
7
+ Fiber.yield
8
+ end
9
+ puts "fib(#{n}) == #{ov}"
10
+ ov
11
+ end
12
+ end
data/bin/test-pool.rb ADDED
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'particle_pool'
5
+
6
+ load File.dirname(__FILE__) + '/task.rb'
7
+
8
+ p = ParticlePool::Pool.new
9
+ p.start
10
+
11
+ $sz = 0
12
+ $arr = []
13
+
14
+ (1024 * Etc.nprocessors).times do |i|
15
+ t = ParticlePool::Task.new do |n|
16
+ raise 'fib not defined for negative numbers' if n < 0
17
+ nv, ov = 1, 0
18
+ n.times do
19
+ nv, ov = nv + ov, nv
20
+ Fiber.yield
21
+ end
22
+ $arr << ov
23
+ $sz += 1
24
+ ov
25
+ end
26
+
27
+ p.push(t, i)
28
+ end
29
+
30
+ t = ParticlePool::Task.new do
31
+ sleep 16
32
+ 'Hello, World!'
33
+ end
34
+ p.push(t)
35
+
36
+ puts t.await
37
+
38
+ while $sz != (1024 * Etc.nprocessors) do
39
+ sleep 1
40
+ end
41
+
42
+ puts $arr.reduce(:+)
data/bin/test-task.rb ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'particle_pool'
5
+
6
+ load File.dirname(__FILE__) + '/task.rb'
7
+
8
+ t = gettask
9
+
10
+ t.start 10
11
+ until t.done? do
12
+ t.resume
13
+ end
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'particle_pool'
5
+
6
+ load File.dirname(__FILE__) + '/task.rb'
7
+
8
+ pt = ParticlePool::PoolThread.new
9
+
10
+ 64.times do |i|
11
+ t = gettask
12
+
13
+ pt.push(t, i)
14
+ end
15
+
16
+ Thread.new do
17
+ pt.start
18
+ end
19
+
20
+ loop do
21
+ sleep 1
22
+ end
@@ -0,0 +1,26 @@
1
+ class ParticlePool::Pool
2
+ def initialize
3
+ @threads = []
4
+ end
5
+
6
+ def <<(t, *args, **kwargs)
7
+ push(t, *args, **kwargs)
8
+ end
9
+
10
+ def push(t, *args, **kwargs)
11
+ thr = @threads.min
12
+ raise 'no threads available' unless t
13
+ thr.push(t, *args, **kwargs)
14
+ end
15
+
16
+ def start(size = Etc.nprocessors)
17
+ size.times do
18
+ @threads << ParticlePool::PoolThread.new
19
+ end
20
+ @threads.each do |t|
21
+ Thread.new do
22
+ t.start
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,75 @@
1
+ class ParticlePool::PoolThread
2
+ include Comparable
3
+
4
+ attr_reader :tasks
5
+
6
+ def initialize
7
+ @queue = Queue.new
8
+ @tasks = 0
9
+ end
10
+
11
+ def <<(t, *args, **kwargs)
12
+ push(t, *args, **kwargs)
13
+ end
14
+
15
+ def push(t, *args, **kwargs)
16
+ @tasks += 1
17
+ @queue << {
18
+ task: t,
19
+ args: args,
20
+ kwargs: kwargs
21
+ }
22
+ end
23
+
24
+ def <=>(other)
25
+ self.tasks <=> other.tasks
26
+ end
27
+
28
+ def start
29
+ tasks = []
30
+ iter = tasks.each
31
+ loop do
32
+ nt = []
33
+
34
+ shouldpop = lambda do
35
+ return true unless @queue.empty?
36
+ return true if tasks.empty? && nt.empty?
37
+ false
38
+ end
39
+
40
+ shouldblock = lambda do
41
+ return true if @queue.empty? && (!tasks.empty? || !nt.empty?)
42
+ false
43
+ end
44
+
45
+ while shouldpop.()
46
+ begin
47
+ i = @queue.pop shouldblock.()
48
+ nt << i
49
+ rescue ThreadError
50
+ break
51
+ end
52
+ end
53
+ nt.each do |i|
54
+ begin
55
+ t = i[:task]
56
+ t.start(*i[:args], **i[:kwargs])
57
+ tasks << t
58
+ rescue => e
59
+ puts e.backtrace
60
+ puts e
61
+ end
62
+ end
63
+ begin
64
+ t = iter.next
65
+ t.resume
66
+ if t.done?
67
+ @tasks -= 1
68
+ tasks.delete t
69
+ end
70
+ rescue StopIteration
71
+ iter.rewind
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,58 @@
1
+ class ParticlePool::Task
2
+ attr_reader :value, :status
3
+
4
+ module Status
5
+ NOT_STARTED = 0
6
+ ALIVE = 1
7
+ DEAD = 2
8
+ PAUSED = 3
9
+ end
10
+
11
+ def initialize(&b)
12
+ @value = nil
13
+ @status = Status::NOT_STARTED
14
+ @b = b
15
+ end
16
+
17
+ def start(*params)
18
+ raise 'cannot start already started task' if @status != Status::NOT_STARTED
19
+ @f = Fiber.new do |*params|
20
+ @status = Status::ALIVE
21
+ @b.(*params)
22
+ end
23
+ @value = @f.resume(*params)
24
+ _upd()
25
+ @status
26
+ end
27
+
28
+ def resume
29
+ if @status == Status::PAUSED
30
+ @status = Status::ALIVE
31
+ @value = @f.resume(@value)
32
+ end
33
+ _upd()
34
+ @status
35
+ end
36
+
37
+ def alive?
38
+ @f.alive?
39
+ end
40
+
41
+ def done?
42
+ @status == Status::DEAD
43
+ end
44
+
45
+ def await(s=0.01)
46
+ sleep(s) until done?
47
+ @value
48
+ end
49
+
50
+ def _upd
51
+ if alive?
52
+ @status = Status::PAUSED
53
+ else
54
+ @status = Status::DEAD
55
+ end
56
+ end
57
+
58
+ end
@@ -0,0 +1,3 @@
1
+ module ParticlePool
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,11 @@
1
+ module ParticlePool
2
+ end
3
+
4
+ require 'etc'
5
+ require 'fiber'
6
+ require 'thread'
7
+
8
+ require 'particle_pool/version'
9
+ require 'particle_pool/task'
10
+ require 'particle_pool/pool_thread'
11
+ require 'particle_pool/pool'
@@ -0,0 +1,26 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'particle_pool/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'particle_pool'
8
+ spec.version = ParticlePool::VERSION
9
+ spec.authors = ['Nickolay Ilyushin']
10
+ spec.email = ['nickolay02@inbox.ru']
11
+
12
+ spec.summary = 'A thread pool implementation for Ruby'
13
+ spec.description = 'A thread pool implementation for Ruby'
14
+ spec.homepage = 'https://github.com/handicraftsman/particle_pool'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.16'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: particle_pool
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nickolay Ilyushin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-26 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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: A thread pool implementation for Ruby
42
+ email:
43
+ - nickolay02@inbox.ru
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - bin/task.rb
57
+ - bin/test-pool.rb
58
+ - bin/test-task.rb
59
+ - bin/test-thread.rb
60
+ - lib/particle_pool.rb
61
+ - lib/particle_pool/pool.rb
62
+ - lib/particle_pool/pool_thread.rb
63
+ - lib/particle_pool/task.rb
64
+ - lib/particle_pool/version.rb
65
+ - particle_pool.gemspec
66
+ homepage: https://github.com/handicraftsman/particle_pool
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.7.7
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: A thread pool implementation for Ruby
90
+ test_files: []