simple_thread_pool 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3ebc46aeec19e5a7572363d8a16e3cb76a1120bfecd31467cc8c232fb5c2480c
4
+ data.tar.gz: 93ca2bddde18af8b14c88c2b6d346966c115d398f575467c1d5989b7025af80e
5
+ SHA512:
6
+ metadata.gz: b14225a854eccedd484d1230dcc57276ee1e71cee72654d4f3030d42c9882c3a8321bdff8b148980b9e8662f8f497e814013368ec5eb26d5894807af5e7738f5
7
+ data.tar.gz: b8f1358b7615e9ab485416222b74fd60c8557f7462fa4088127c3aedc4b7dddc1bbcdea8a4a961ff07de73d4a4a2b8044cd05b95a006c3fc07768713a24c3f08
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,3 @@
1
+ # 1.0.0
2
+
3
+ * Initial release
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 simple_thread_pool.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Brian Durand
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,76 @@
1
+ # SimpleThreadPool
2
+
3
+ Simple implementation of the thread pool to manage executing tasks in parallel. The thread pool implemented by this code is designed to allow throttled, parallel execution of tasks.
4
+
5
+ Unlike some thread pools, this code does not maintain an internal queue of tasks. Instead, it simply blocks until a thread is available. This ensures that the pool memory size doesn't become bloated with 1000's of enqueued blocks of code.
6
+
7
+ The threads created for running the tasks are short lived threads and are not reused by the pool. This ensures that no thread local variables can accidentally be reused between threads.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'simple_thread_pool'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install simple_thread_pool
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ # Create a pool of 10 threads to work with.
29
+ thread_pool = SimpleThreadPoolnew(10)
30
+
31
+ # Execute a block of code in a thread.
32
+ # If there are no free threads in the pool, then this will block until one is freed up.
33
+ thread_pool.execute do
34
+ # Do some work here
35
+ end
36
+
37
+ # Execute a block of code with an identifier.
38
+ # The thread pool will not run code with the same identifier in parallel
39
+ # and will execute them in the order they are called
40
+ thread_pool.execute("foo") do
41
+ # Do some work here
42
+ end
43
+
44
+ # Block until all threads have finished executing.
45
+ # You should always call this method after all calls to `execute` have been made
46
+ thread_pool.finish
47
+ ```
48
+
49
+ ### Error handling.
50
+
51
+ All error handling must be conducted inside the execute block. The main thread will not be notified of any exceptions. You can use the `synchronized` method on the thread pool if you need to work data from the main thread.
52
+
53
+ Example of how you can track any errors in a shared array.
54
+
55
+ ```ruby
56
+ errors = []
57
+
58
+ thread_pool.execute do
59
+ begin
60
+ # Do something
61
+ rescue Error => e
62
+ thread_pool.synchronized { errors << e }
63
+ raise e
64
+ end
65
+ end
66
+
67
+ thread_pool.finish
68
+ ```
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bdurand/simple_thread_pool.
73
+
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'thread'
4
+ require 'set'
5
+
6
+ # Simple thread pool for executing blocks in parallel in a controlled manner.
7
+ # Threads are not re-used by the pool to prevent any thread local variables from
8
+ # leaking out.
9
+ class SimpleThreadPool
10
+
11
+ def initialize(max_threads)
12
+ @max_threads = max_threads
13
+ @lock = Mutex.new
14
+ @threads = []
15
+ @processing_ids = []
16
+ end
17
+
18
+ # Call this method to spawn a thread to run the block. If the thread pool
19
+ # is already full, this method will block until a thread is free. The block
20
+ # is responsible for handling any exceptions that could be raised.
21
+ #
22
+ # The optional id argument can be used to provide an identifier for a block.
23
+ # If one is provided, processing will be blocked if the same id is already
24
+ # being processed. This ensures that each unique id is executed one at a time
25
+ # sequentially.
26
+ def execute(id = nil, &block)
27
+ loop do
28
+ # Check if a new thread can be added without blocking.
29
+ while !can_add_thread?(id)
30
+ sleep(0.001)
31
+ end
32
+
33
+ @lock.synchronize do
34
+ # Check again inside a synchronized block if the thread can still be added.
35
+ if can_add_thread?(id)
36
+ @processing_ids << id unless id.nil?
37
+ add_thread(id, block)
38
+ return
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ # Call this method to block until all current threads have finished executing.
45
+ def finish
46
+ active_threads = @lock.synchronize { @threads.select(&:alive?) }
47
+ active_threads.each(&:join)
48
+ nil
49
+ end
50
+
51
+ # Synchronize data access across the thread pool. This method will block
52
+ # waiting on the same internal Mutex the thread pool uses to manage scheduling
53
+ # threads.
54
+ def synchronize(&block)
55
+ @lock.synchronize(&block)
56
+ end
57
+
58
+ private
59
+
60
+ def can_add_thread?(id)
61
+ @threads.size < @max_threads && (id.nil? || !@processing_ids.include?(id))
62
+ end
63
+
64
+ # Spawn a thread in this method to ensure that it doesn't accidentally pick up any local variables.
65
+ def add_thread(id, block)
66
+ main_thread = Thread.current
67
+
68
+ @threads << Thread.new do
69
+ begin
70
+ block.call
71
+ # Return nil to ensure no objects are leaked.
72
+ nil
73
+ ensure
74
+ @lock.synchronize do
75
+ @processing_ids.delete(id) unless id.nil?
76
+ @threads.delete(Thread.current)
77
+ end
78
+ main_thread.wakeup if main_thread.alive?
79
+ end
80
+ end
81
+
82
+ nil
83
+ end
84
+
85
+ end
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "simple_thread_pool"
3
+ spec.version = File.read(File.expand_path("VERSION", __dir__)).chomp
4
+ spec.authors = ["Brian Durand"]
5
+ spec.email = ["bbdurand@gmail.com"]
6
+
7
+ spec.summary = %q{Simple thread pool implementation to manage running tasks in parallel.}
8
+ spec.homepage = "https://github.com/bdurand/simple_thread_pool"
9
+ spec.license = "MIT"
10
+
11
+ # Specify which files should be added to the gem when it is released.
12
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
13
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
14
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
+ end
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.16"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec", "~> 3.8"
23
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_thread_pool
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Durand
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-02-08 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
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.8'
55
+ description:
56
+ email:
57
+ - bbdurand@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - CHANGE_LOG.md
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - VERSION
69
+ - lib/simple_thread_pool.rb
70
+ - simple_thread_pool.gemspec
71
+ homepage: https://github.com/bdurand/simple_thread_pool
72
+ licenses:
73
+ - MIT
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.7.6
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Simple thread pool implementation to manage running tasks in parallel.
95
+ test_files: []