parallel_tasker 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/parallel_tasker.rb +71 -0
- metadata +59 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0d8fe4c994ffe659c9948b4d3730eae83f855f68
|
4
|
+
data.tar.gz: 92e3a0c46fc26fd7d70f1c2ef8b22ad8da806980
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 356927b1f3c8491cb77276bb837f420fa67bfdf816c63695d1c7b461beb8857626bebfb40e2a3945de1ee6b0e9cfb5503cbf0df35e84712b826a4c1bca588818
|
7
|
+
data.tar.gz: 989da5fe6193e68cefb4d08a783e0d952b1c6b442fb18f5c749e799c0fbaf319426e465dcdc4b03852bae858aa68464e3fcefe7e984ffbb245df0e7cd3be8628
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'thwait'
|
2
|
+
|
3
|
+
# Run tasks in parallel threads
|
4
|
+
class ParallelTasker
|
5
|
+
|
6
|
+
# Set max number of parallel threads
|
7
|
+
def initialize limit
|
8
|
+
@limit = limit
|
9
|
+
@tasks = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
# Add task to be executed.
|
13
|
+
def add_task id, &task
|
14
|
+
@tasks[id] = task
|
15
|
+
end
|
16
|
+
|
17
|
+
alias_method :<<, :add_task
|
18
|
+
|
19
|
+
# Return block for task with given id
|
20
|
+
def task id
|
21
|
+
@tasks[id]
|
22
|
+
end
|
23
|
+
|
24
|
+
# Execute all tasks in separate threads, with maximum asked limit of parallel
|
25
|
+
# threads.
|
26
|
+
# Returns a Hash with all given id as keys, and its value are threads
|
27
|
+
# themselves. User can run Thread#status to see if it terminated with an
|
28
|
+
# exception (nil) or not (false), and Thread#value to get either its return
|
29
|
+
# value or returned exception.
|
30
|
+
def run
|
31
|
+
@threads = {}
|
32
|
+
@limit = @tasks.size if @limit > @tasks.size
|
33
|
+
pending_ids = @tasks.keys
|
34
|
+
@running_ids = []
|
35
|
+
completed_ids = []
|
36
|
+
# start initial batch
|
37
|
+
pending_ids.shift(@limit).each{|id| new_thread(id)}
|
38
|
+
# wait for termination
|
39
|
+
twait = ThreadsWait.new(*running_threads)
|
40
|
+
twait.all_waits do |finished_thread|
|
41
|
+
# update arrays
|
42
|
+
completed_id = @threads.key(finished_thread)
|
43
|
+
@running_ids.delete completed_id
|
44
|
+
completed_ids << completed_id
|
45
|
+
# start new thread if available and below limit
|
46
|
+
if not pending_ids.empty? and @running_ids.size < @limit
|
47
|
+
new_id = pending_ids.shift
|
48
|
+
new_thread new_id
|
49
|
+
twait.join_nowait *running_threads
|
50
|
+
end
|
51
|
+
end
|
52
|
+
@threads
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
# Create a new thread based on given id
|
58
|
+
def new_thread id
|
59
|
+
@threads[id] = Thread.new &@tasks[id]
|
60
|
+
@running_ids << id
|
61
|
+
end
|
62
|
+
|
63
|
+
# return array of all running threads
|
64
|
+
def running_threads
|
65
|
+
rt = []
|
66
|
+
@running_ids.each do |id|
|
67
|
+
rt << @threads[id]
|
68
|
+
end
|
69
|
+
rt
|
70
|
+
end
|
71
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: parallel_tasker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Fabio Pugliese Ornellas
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.2'
|
27
|
+
description: Simple Gem that collects tasks to be run, then execute them with maximum
|
28
|
+
specified parallelism.
|
29
|
+
email: fabio.ornellas@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- lib/parallel_tasker.rb
|
35
|
+
homepage: https://github.com/fornellas/parallel_tasker
|
36
|
+
licenses:
|
37
|
+
- GPL
|
38
|
+
metadata: {}
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 2.4.5
|
56
|
+
signing_key:
|
57
|
+
specification_version: 4
|
58
|
+
summary: Simple Gem to help run tasks in parallel
|
59
|
+
test_files: []
|