parallel_tasker 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/parallel_tasker.rb +25 -43
- data/lib/parallel_tasker/task.rb +18 -0
- metadata +37 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6dd857a5fca48478d3c8cfaa8c8ee6084e124c3d
|
4
|
+
data.tar.gz: eb266b7089b264a51a8af2797686e7820cdc6c9e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd5657fce49273dc76034370db145b5137f979b5b655e5baa35e3c460c7c263529198c463423b6e9466c13e3048a07be1e063179773d0b929aa1107ee1f0f55a
|
7
|
+
data.tar.gz: 89d67e1dcf2da7c7bcc431384ef067508d9418b201fb8d2c9dc784199ccb5dfa306c156dedbf70ac98e657abf5eaa1edfcb1c879c95ecab194f745aca8bb12a8
|
data/lib/parallel_tasker.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'thwait'
|
2
2
|
|
3
|
+
require_relative 'parallel_tasker/task'
|
4
|
+
|
3
5
|
# Run tasks in parallel threads
|
4
6
|
class ParallelTasker
|
5
7
|
|
@@ -8,7 +10,7 @@ class ParallelTasker
|
|
8
10
|
# Unless block calls #run, it is called automatically.
|
9
11
|
def initialize concurrency
|
10
12
|
@concurrency = concurrency
|
11
|
-
@
|
13
|
+
@tasks_queue = Queue.new
|
12
14
|
@already_run = false
|
13
15
|
if block_given?
|
14
16
|
yield self
|
@@ -17,17 +19,12 @@ class ParallelTasker
|
|
17
19
|
end
|
18
20
|
|
19
21
|
# Add task to be executed.
|
20
|
-
def add_task id, &
|
21
|
-
@
|
22
|
+
def add_task id, &block
|
23
|
+
@tasks_queue << Task.new(id, &block)
|
22
24
|
end
|
23
25
|
|
24
26
|
alias_method :<<, :add_task
|
25
27
|
|
26
|
-
# Return block for task with given id
|
27
|
-
def task id
|
28
|
-
@tasks[id]
|
29
|
-
end
|
30
|
-
|
31
28
|
class DoubleRun < RuntimeError ; end
|
32
29
|
|
33
30
|
# Execute all tasks in separate threads, with maximum asked concurrency of parallel
|
@@ -37,44 +34,29 @@ class ParallelTasker
|
|
37
34
|
def run
|
38
35
|
raise DoubleRun.new('#run called more than one time') if @already_run
|
39
36
|
@already_run = true
|
40
|
-
@
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
completed_ids = []
|
45
|
-
# start initial batch
|
46
|
-
pending_ids.shift(@concurrency).each{|id| new_thread(id)}
|
47
|
-
# wait for termination
|
48
|
-
twait = ThreadsWait.new(*running_threads)
|
49
|
-
twait.all_waits do |finished_thread|
|
50
|
-
# update arrays
|
51
|
-
completed_id = @threads.key(finished_thread)
|
52
|
-
@already_running_ids.delete completed_id
|
53
|
-
completed_ids << completed_id
|
54
|
-
# start new thread if available and below concurrency
|
55
|
-
if not pending_ids.empty? and @already_running_ids.size < @concurrency
|
56
|
-
new_id = pending_ids.shift
|
57
|
-
new_thread new_id
|
58
|
-
twait.join_nowait *running_threads
|
59
|
-
end
|
37
|
+
@result = {}
|
38
|
+
processor_threads = []
|
39
|
+
@concurrency.times do
|
40
|
+
processor_threads << new_processor_thread
|
60
41
|
end
|
61
|
-
|
42
|
+
processor_threads.each{|t| t.join }
|
43
|
+
@result
|
62
44
|
end
|
63
|
-
|
45
|
+
|
64
46
|
private
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
47
|
+
|
48
|
+
def new_processor_thread
|
49
|
+
Thread.new do
|
50
|
+
until @tasks_queue.empty?
|
51
|
+
task = @tasks_queue.pop
|
52
|
+
thread = Thread.new{task.block.call}
|
53
|
+
@result[task.id] = thread
|
54
|
+
begin
|
55
|
+
thread.join
|
56
|
+
rescue
|
57
|
+
end
|
58
|
+
end
|
77
59
|
end
|
78
|
-
rt
|
79
60
|
end
|
61
|
+
|
80
62
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class ParallelTasker
|
2
|
+
# Task to be run
|
3
|
+
class Task
|
4
|
+
|
5
|
+
attr_accessor :id, :block
|
6
|
+
|
7
|
+
# Raised when no block was given
|
8
|
+
class NoBlockGiven < RuntimeError ; end
|
9
|
+
|
10
|
+
# Receive a task id, an a block with the task
|
11
|
+
def initialize id, &block
|
12
|
+
raise NoBlockGiven.new("No block given") unless block
|
13
|
+
@id = id
|
14
|
+
@block = block
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parallel_tasker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fabio Pugliese Ornellas
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -24,6 +24,40 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: guard
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.12'
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 2.12.5
|
37
|
+
type: :development
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - "~>"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '2.12'
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.12.5
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: guard-rspec
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '4.5'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '4.5'
|
27
61
|
description: Collects tasks to be run, then execute in parallel, with maximum specified
|
28
62
|
concurrency.
|
29
63
|
email: fabio.ornellas@gmail.com
|
@@ -32,6 +66,7 @@ extensions: []
|
|
32
66
|
extra_rdoc_files: []
|
33
67
|
files:
|
34
68
|
- lib/parallel_tasker.rb
|
69
|
+
- lib/parallel_tasker/task.rb
|
35
70
|
homepage: https://github.com/fornellas/parallel_tasker
|
36
71
|
licenses:
|
37
72
|
- GPL
|