parallelizer 0.0.1-java
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 +7 -0
- data/lib/parallelizer/org.rubygems.parallelizer.jar +0 -0
- data/lib/parallelizer.rb +112 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f10a7c799b97276e92c5fc8c7cf67fb96f889054
|
4
|
+
data.tar.gz: b04721afd9fd5dcff3adb8d323b144b1e5f209a3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 061daf70da2ca3137d942070323c8236f9bbe9128a801e8b1150b6d78615aa7933e1d5cc2f0b845c55d02f53b643090e948ba69308126c2f46b3cc9049b8dd39
|
7
|
+
data.tar.gz: ea9f2436eac22b671ae060f0038ac876c492765449dc701c232740ea70ed0c6f6b761a3dbf64811b89994538d5de8d4ae28162411f79dae1b8450c863142bf5c
|
Binary file
|
data/lib/parallelizer.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
# used to call java code
|
4
|
+
require 'java'
|
5
|
+
|
6
|
+
# 'java_import' is used to import java classes
|
7
|
+
java_import 'java.util.concurrent.Callable'
|
8
|
+
java_import 'java.util.concurrent.FutureTask'
|
9
|
+
java_import 'java.util.concurrent.LinkedBlockingQueue'
|
10
|
+
java_import 'java.util.concurrent.ThreadPoolExecutor'
|
11
|
+
java_import 'java.util.concurrent.TimeUnit'
|
12
|
+
|
13
|
+
# get our DefaultDaemonThreadFactory class, so that the process will be able to end even
|
14
|
+
# if we haven't explicitly shut down the pool (the default thread factory marks its threads as
|
15
|
+
# non-daemon, which keeps the JVM from exiting until they are explicitly shut down)
|
16
|
+
require "parallelizer/org.rubygems.parallelizer.jar"
|
17
|
+
java_import 'org.rubygems.parallelizer.DefaultDaemonThreadFactory'
|
18
|
+
|
19
|
+
|
20
|
+
class Parallelizer
|
21
|
+
attr_accessor :max_acceptable_delay, :delayed_too_long_proc
|
22
|
+
|
23
|
+
def initialize ops={} #:core_pool_threads, :max_pool_threads, :keep_alive_time, :max_acceptable_delay, :delayed_too_long_proc, :prestart_all_core_threads
|
24
|
+
core_pool_threads = ops[:core_pool_threads] || 10
|
25
|
+
max_pool_threads = ops[:max_pool_threads] || 10
|
26
|
+
raise "Parallelizer core_pool_threads greater than max_pool_threads!" if core_pool_threads > max_pool_threads
|
27
|
+
|
28
|
+
@pool = ThreadPoolExecutor.new(core_pool_threads,
|
29
|
+
max_pool_threads,
|
30
|
+
ops[:keep_alive_time] || 60,
|
31
|
+
TimeUnit::SECONDS,
|
32
|
+
LinkedBlockingQueue.new,
|
33
|
+
DefaultDaemonThreadFactory.new)
|
34
|
+
|
35
|
+
@max_acceptable_delay = ops[:max_acceptable_delay]
|
36
|
+
@delayed_too_long_proc = ops[:delayed_too_long_proc]
|
37
|
+
|
38
|
+
prestart_all_core_threads if ops[:prestart_all_core_threads]
|
39
|
+
end
|
40
|
+
|
41
|
+
def prestart_all_core_threads
|
42
|
+
@pool.prestartAllCoreThreads
|
43
|
+
end
|
44
|
+
|
45
|
+
def shutdown
|
46
|
+
@pool.shutdown
|
47
|
+
end
|
48
|
+
|
49
|
+
#works like a normal map, but in parallel, and also if an exception is raised that exception
|
50
|
+
#will be stored in that index instead of the result
|
51
|
+
def map enumerator, &proc
|
52
|
+
run_computation_array enumerator.map {|arg| Computation.new(self, proc, arg) }
|
53
|
+
end
|
54
|
+
|
55
|
+
#expects an array of procs
|
56
|
+
def run array
|
57
|
+
run_computation_array array.map {|proc| Computation.new(self, proc) }
|
58
|
+
end
|
59
|
+
|
60
|
+
protected
|
61
|
+
|
62
|
+
# Implement a callable class
|
63
|
+
class Computation
|
64
|
+
include Callable
|
65
|
+
|
66
|
+
attr_accessor :results_array, :result_index, :execution_requested_time
|
67
|
+
|
68
|
+
def initialize parallelizer, proc, argument=nil
|
69
|
+
@parallelizer = parallelizer
|
70
|
+
@proc = proc
|
71
|
+
@argument = argument
|
72
|
+
end
|
73
|
+
|
74
|
+
def call
|
75
|
+
begin
|
76
|
+
test_delay
|
77
|
+
@results_array[@result_index] = @argument ? @proc.call(@argument) : @proc.call
|
78
|
+
rescue
|
79
|
+
@results_array[@result_index] = $!
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_delay
|
84
|
+
delay = nil
|
85
|
+
if @parallelizer.max_acceptable_delay && @parallelizer.delayed_too_long_proc && @execution_requested_time
|
86
|
+
(delay = Time.now.to_f - @execution_requested_time.to_f) > @parallelizer.max_acceptable_delay
|
87
|
+
@parallelizer.delayed_to_long_proc.call(delay)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def run_computation_array computation_array
|
93
|
+
results_array = Array.new(computation_array.size)
|
94
|
+
future_tasks = []
|
95
|
+
computation_array.each_with_index {|comp, i|
|
96
|
+
comp.results_array = results_array
|
97
|
+
comp.result_index = i
|
98
|
+
comp.execution_requested_time = Time.now
|
99
|
+
|
100
|
+
if i < computation_array.size - 1
|
101
|
+
task = FutureTask.new(comp)
|
102
|
+
@pool.execute task
|
103
|
+
future_tasks << task
|
104
|
+
else #last element, run it in this thread
|
105
|
+
comp.call
|
106
|
+
end
|
107
|
+
}
|
108
|
+
|
109
|
+
future_tasks.each {|task| task.get } #wait for all tasks to complete
|
110
|
+
results_array
|
111
|
+
end
|
112
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: parallelizer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: java
|
6
|
+
authors:
|
7
|
+
- Mohamed Hafez
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-28 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Run commands in parallel in JRuby, on a reusable thread pool
|
14
|
+
email:
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/parallelizer.rb
|
20
|
+
- lib/parallelizer/org.rubygems.parallelizer.jar
|
21
|
+
homepage: https://github.com/mohamedhafez/parallelizer
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.2.2
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: Run commands in parallel in JRuby, on a reusable thread pool
|
45
|
+
test_files: []
|