pwork 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/pwork.rb +11 -0
- data/lib/pwork/array.rb +47 -0
- data/lib/pwork/helpers/thread.rb +26 -0
- data/lib/pwork/jruby/thread_pool.rb +25 -0
- data/lib/pwork/mri/thread_pool.rb +31 -0
- data/lib/pwork/version.rb +3 -0
- data/lib/pwork/worker.rb +22 -0
- metadata +11 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2731486c2af84b607e31651780ca3b57511e1496
|
4
|
+
data.tar.gz: ddbe9066147e3a962302493eecc992541a221290
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 75958149fd8a60da17ff86b7e1d2a9750944d1f4a5e5bc0177a416f6ab7c4795f46350cd694395414813112859684cd456c57fb8e0613e9e33e8f7d2149a7a38
|
7
|
+
data.tar.gz: 25fc545ed0139c580d5f5f8fcf6fd80b87cd17688603882984421729480feeb1b332e482cda1c01f1b69911599e8f301a1e3b693d2e0e6d2d37ff2057a20fd17
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "pwork"
|
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
|
data/bin/setup
ADDED
data/lib/pwork.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'pwork/version'
|
2
|
+
require_relative 'pwork/helpers/thread'
|
3
|
+
|
4
|
+
require_relative 'pwork/array'
|
5
|
+
require_relative 'pwork/worker'
|
6
|
+
|
7
|
+
if RUBY_PLATFORM =~ /java/
|
8
|
+
require_relative 'pwork/jruby/thread_pool'
|
9
|
+
else
|
10
|
+
require_relative 'pwork/mri/thread_pool'
|
11
|
+
end
|
data/lib/pwork/array.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
class Array
|
2
|
+
|
3
|
+
#This method is called to loop over each item in this array in parallel threads
|
4
|
+
def peach(thread_count = 5, &block)
|
5
|
+
@mutex = Mutex.new
|
6
|
+
@index_count = -1
|
7
|
+
threads = []
|
8
|
+
|
9
|
+
thread_vars = PWork::Helpers::Threads.get_thread_vars
|
10
|
+
|
11
|
+
thread_count.times.each do
|
12
|
+
thread = Thread.new do
|
13
|
+
PWork::Helpers::Threads.set_thread_vars(thread_vars)
|
14
|
+
thread_process(&block)
|
15
|
+
end
|
16
|
+
threads.push(thread)
|
17
|
+
end
|
18
|
+
#wait for all threads to exit
|
19
|
+
threads.each { |thr| thr.join }
|
20
|
+
end
|
21
|
+
|
22
|
+
#This method is used to recursively process an item on a thread
|
23
|
+
def thread_process(&block)
|
24
|
+
|
25
|
+
item = nil
|
26
|
+
|
27
|
+
#use a mutex to get the next item from the array to ensure thread safety
|
28
|
+
@mutex.synchronize do
|
29
|
+
@index_count += 1
|
30
|
+
|
31
|
+
if @index_count <= self.length - 1
|
32
|
+
item = self[@index_count]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
if item == nil
|
37
|
+
return
|
38
|
+
end
|
39
|
+
|
40
|
+
#execute the block for the current item
|
41
|
+
yield item
|
42
|
+
|
43
|
+
#recursively attempt to process another item from the array opn this thread
|
44
|
+
thread_process(&block)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module PWork
|
2
|
+
module Helpers
|
3
|
+
class Threads
|
4
|
+
def self.get_thread_vars
|
5
|
+
vars = {}
|
6
|
+
Thread.current.keys.each do |k|
|
7
|
+
vars[k] = Thread.current[k]
|
8
|
+
end
|
9
|
+
vars
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.set_thread_vars(vars)
|
13
|
+
PWork::Helpers::Threads.reset_thread_vars
|
14
|
+
vars.keys.each do |k|
|
15
|
+
Thread.current[k] = vars[k]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.reset_thread_vars
|
20
|
+
Thread.current.keys.each do |k|
|
21
|
+
Thread.current[k] = nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'java'
|
2
|
+
java_import java.util.concurrent.Executors
|
3
|
+
module PWork
|
4
|
+
class ThreadPool
|
5
|
+
|
6
|
+
def initialize(threads: 5)
|
7
|
+
@processes = []
|
8
|
+
@executor = java.util.concurrent.Executors::newFixedThreadPool threads
|
9
|
+
end
|
10
|
+
|
11
|
+
#This method is called to pass a block to be ran on a thread within the thread pool
|
12
|
+
def execute(&block)
|
13
|
+
thread_vars = PWork::Helpers::Threads.get_thread_vars
|
14
|
+
@executor.execute do
|
15
|
+
PWork::Helpers::Threads.set_thread_vars(thread_vars)
|
16
|
+
block.call
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def shutdown
|
21
|
+
@executor.shutdown
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module PWork
|
2
|
+
class ThreadPool
|
3
|
+
def initialize(threads: 5)
|
4
|
+
@size = threads
|
5
|
+
@jobs = Queue.new
|
6
|
+
@pool = Array.new(threads) do
|
7
|
+
Thread.new do
|
8
|
+
catch(:exit) do
|
9
|
+
loop do
|
10
|
+
job, thread_vars = @jobs.pop
|
11
|
+
PWork::Helpers::Threads.set_thread_vars(thread_vars)
|
12
|
+
job.call
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def execute(&block)
|
20
|
+
@jobs << [block, PWork::Helpers::Threads.get_thread_vars]
|
21
|
+
end
|
22
|
+
|
23
|
+
def shutdown
|
24
|
+
@size.times do
|
25
|
+
execute { throw :exit }
|
26
|
+
end
|
27
|
+
|
28
|
+
@pool.map(&:join)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/pwork/worker.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module PWork
|
2
|
+
class Worker
|
3
|
+
|
4
|
+
def initialize(threads: 5)
|
5
|
+
@processes = []
|
6
|
+
@thread_count = threads
|
7
|
+
end
|
8
|
+
|
9
|
+
#This method is called to add a process to this worker
|
10
|
+
def add(&block)
|
11
|
+
@processes.push(block)
|
12
|
+
end
|
13
|
+
|
14
|
+
#This method is called to execute all processes attached to this worker in parallel on the specified number of threads.
|
15
|
+
def execute
|
16
|
+
@processes.peach(@thread_count) do |item|
|
17
|
+
item.call
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pwork
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- vaughanbrittonage
|
@@ -73,7 +73,16 @@ email:
|
|
73
73
|
executables: []
|
74
74
|
extensions: []
|
75
75
|
extra_rdoc_files: []
|
76
|
-
files:
|
76
|
+
files:
|
77
|
+
- bin/console
|
78
|
+
- bin/setup
|
79
|
+
- lib/pwork.rb
|
80
|
+
- lib/pwork/array.rb
|
81
|
+
- lib/pwork/helpers/thread.rb
|
82
|
+
- lib/pwork/jruby/thread_pool.rb
|
83
|
+
- lib/pwork/mri/thread_pool.rb
|
84
|
+
- lib/pwork/version.rb
|
85
|
+
- lib/pwork/worker.rb
|
77
86
|
homepage: https://github.com/vaughanbrittonsage/pwork
|
78
87
|
licenses:
|
79
88
|
- MIT
|