arb-thread 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0f91dd6b00e99650806f28a4b3f2517758321841
4
+ data.tar.gz: aed9c5149f8476ef75540f01e0eed1e39440c163
5
+ SHA512:
6
+ metadata.gz: 9c111480dbf0b660d4fd71843a671fe3dd477be2b6006f3633c49600f8f99902ac18b9ef54596710e25032ca1fe01dcc5cefa81e518be2cbd57e677e011a720e
7
+ data.tar.gz: 2c3b8fe2824eb26d27b0dc39043fad020ea6e9be2e44e42d5b64a558c3a5df3d872c4de6ed9b1faa9b35394b3890dd08621a389453f72e0e3ce3a29b0dc8de52
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in arb-thread.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # Arb::Thread
2
+
3
+ A simple "thread pool" for parallel tasks.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'arb-thread'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install arb-thread
20
+
21
+ ## Usage
22
+
23
+ To illustrate, let's take the example of "The Ticket model".
24
+
25
+ ```ruby
26
+ require 'arb/thread'
27
+
28
+ #Generate 8000 tickets here.
29
+ source=(1..8000).to_a.map{|i| "ticket - #{i}"}
30
+
31
+ #Use 30 ticket windows to sell tickets in parallel(30 threads in fact).
32
+ Thread.parallel(30) do |dispatcher|
33
+ 8000.times do
34
+ dispatcher.new_task do |lock|
35
+ #The lock is a just a Mutex instance stored in dispatcher for handy usage.
36
+ #You can use an external Mutex to synchronize if you like.
37
+ lock.synchronize do
38
+ #Sell ticket here.
39
+ puts source.pop
40
+ end
41
+ sleep 3+rand(3)
42
+ end
43
+ end
44
+
45
+ end
46
+ ```
47
+
48
+ For detailed usage, run
49
+
50
+ $ gem unpack arb-thread
51
+
52
+ and dig into the class Arb::Thread::TaskDispatcher.
53
+
54
+ ## Development
55
+
56
+ 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.
57
+
58
+ 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).
59
+
60
+ ## Contributing
61
+
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/arybin/arb-thread.
63
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'arb/thread/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "arb-thread"
8
+ spec.version = Arb::Thread::VERSION
9
+ spec.authors = ["arybin"]
10
+ spec.email = ["arybin@163.com"]
11
+
12
+ spec.summary = %q{A simple "thread pool" for parallel tasks.}
13
+ spec.description = %q{A simple "thread pool" for parallel tasks.}
14
+ spec.homepage = "https://github.com/arybin-cn/arb-thread"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
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", "~> 1.14"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "arb/thread"
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__)
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
@@ -0,0 +1,5 @@
1
+ module Arb
2
+ module Thread
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
data/lib/arb/thread.rb ADDED
@@ -0,0 +1,70 @@
1
+ require "arb/thread/version"
2
+
3
+ module Arb
4
+ module Thread
5
+ #Simple thread pool.
6
+ class TaskDispatcher
7
+ def initialize(max_thread_count)
8
+ @assist_mutex=Mutex.new
9
+ @task_mutex=Mutex.new
10
+ @max_thread_count=max_thread_count
11
+ @current_thread_count=0
12
+ end
13
+
14
+ def task_sync
15
+ @task_mutex.synchronize do
16
+ yield
17
+ end
18
+ end
19
+
20
+ def task_start(&blk)
21
+ @current_thread_count+=1
22
+ ::Thread.new do
23
+ begin
24
+ blk[@assist_mutex]
25
+ rescue Exception=>e
26
+ puts e
27
+ ensure
28
+ task_end
29
+ end
30
+ end
31
+ end
32
+
33
+ def task_end
34
+ task_sync do
35
+ @current_thread_count-=1
36
+ end
37
+ end
38
+
39
+ def available
40
+ task_sync do
41
+ @current_thread_count
42
+ end
43
+ end
44
+
45
+ def new_task(refresh_delay=0.2, &blk)
46
+ return nil unless blk
47
+ break_loop=false
48
+ loop do
49
+ task_sync do
50
+ if @current_thread_count<@max_thread_count
51
+ break_loop=true
52
+ task_start(&blk)
53
+ end
54
+ end
55
+ break if break_loop
56
+ sleep refresh_delay
57
+ end
58
+ end
59
+ private :task_sync,:task_start,:task_end
60
+ end
61
+
62
+ class << ::Thread
63
+ define_method :parallel do |max_thread_count,&blk|
64
+ TaskDispatcher.new(max_thread_count).tap do |dispatcher|
65
+ blk[dispatcher]
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arb-thread
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - arybin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-21 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
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 simple "thread pool" for parallel tasks.
42
+ email:
43
+ - arybin@163.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - README.md
51
+ - Rakefile
52
+ - arb-thread.gemspec
53
+ - bin/console
54
+ - bin/setup
55
+ - lib/arb/thread.rb
56
+ - lib/arb/thread/version.rb
57
+ homepage: https://github.com/arybin-cn/arb-thread
58
+ licenses: []
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.4.8
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: A simple "thread pool" for parallel tasks.
80
+ test_files: []