parallel_tasker 0.0.0 → 0.1.0
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 +4 -4
- data/lib/parallel_tasker.rb +24 -15
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4d9dc4e614c5092e3e3fb9f9ab98bd4a0e295d4
|
4
|
+
data.tar.gz: 0c78457e01b3501c4f438696c7894427e14635de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ed83725b491608eba9e48f2b81ba2ca838a0ba9e07357496f3fcaf4a1dbedfa1c33b9ae65857493006f754aa15ed4530751387c8f1f3b75d2e9efc3d3e4e2e2b
|
7
|
+
data.tar.gz: 790488766ebfe04a839f02f67390e007d5e763177f495d817211acd4ef71a43a539482cc4b0073069b8d851bc3579bb46d48951d0a66a252e709f96e2def9b3c
|
data/lib/parallel_tasker.rb
CHANGED
@@ -3,10 +3,17 @@ require 'thwait'
|
|
3
3
|
# Run tasks in parallel threads
|
4
4
|
class ParallelTasker
|
5
5
|
|
6
|
-
# Set max number of parallel threads
|
7
|
-
|
8
|
-
|
6
|
+
# Set max number of parallel threads.
|
7
|
+
# Yields self to block, if given.
|
8
|
+
# Unless block calls #run, it is called automatically.
|
9
|
+
def initialize concurrency
|
10
|
+
@concurrency = concurrency
|
9
11
|
@tasks = {}
|
12
|
+
@already_run = false
|
13
|
+
if block_given?
|
14
|
+
yield self
|
15
|
+
self.run unless @already_run
|
16
|
+
end
|
10
17
|
end
|
11
18
|
|
12
19
|
# Add task to be executed.
|
@@ -20,30 +27,32 @@ class ParallelTasker
|
|
20
27
|
def task id
|
21
28
|
@tasks[id]
|
22
29
|
end
|
30
|
+
|
31
|
+
class DoubleRun < RuntimeError ; end
|
23
32
|
|
24
|
-
# Execute all tasks in separate threads, with maximum asked
|
33
|
+
# Execute all tasks in separate threads, with maximum asked concurrency of parallel
|
25
34
|
# threads.
|
26
35
|
# Returns a Hash with all given id as keys, and its value are threads
|
27
|
-
# themselves. User can
|
28
|
-
# exception (nil) or not (false), and Thread#value to get either its return
|
29
|
-
# value or returned exception.
|
36
|
+
# themselves. User can use Thread class methods to verify each task state, such as Thread#join.
|
30
37
|
def run
|
38
|
+
raise DoubleRun.new('#run called more than one time') if @already_run
|
39
|
+
@already_run = true
|
31
40
|
@threads = {}
|
32
|
-
@
|
41
|
+
@concurrency = @tasks.size if @concurrency > @tasks.size
|
33
42
|
pending_ids = @tasks.keys
|
34
|
-
@
|
43
|
+
@already_running_ids = []
|
35
44
|
completed_ids = []
|
36
45
|
# start initial batch
|
37
|
-
pending_ids.shift(@
|
46
|
+
pending_ids.shift(@concurrency).each{|id| new_thread(id)}
|
38
47
|
# wait for termination
|
39
48
|
twait = ThreadsWait.new(*running_threads)
|
40
49
|
twait.all_waits do |finished_thread|
|
41
50
|
# update arrays
|
42
51
|
completed_id = @threads.key(finished_thread)
|
43
|
-
@
|
52
|
+
@already_running_ids.delete completed_id
|
44
53
|
completed_ids << completed_id
|
45
|
-
# start new thread if available and below
|
46
|
-
if not pending_ids.empty? and @
|
54
|
+
# start new thread if available and below concurrency
|
55
|
+
if not pending_ids.empty? and @already_running_ids.size < @concurrency
|
47
56
|
new_id = pending_ids.shift
|
48
57
|
new_thread new_id
|
49
58
|
twait.join_nowait *running_threads
|
@@ -57,13 +66,13 @@ class ParallelTasker
|
|
57
66
|
# Create a new thread based on given id
|
58
67
|
def new_thread id
|
59
68
|
@threads[id] = Thread.new &@tasks[id]
|
60
|
-
@
|
69
|
+
@already_running_ids << id
|
61
70
|
end
|
62
71
|
|
63
72
|
# return array of all running threads
|
64
73
|
def running_threads
|
65
74
|
rt = []
|
66
|
-
@
|
75
|
+
@already_running_ids.each do |id|
|
67
76
|
rt << @threads[id]
|
68
77
|
end
|
69
78
|
rt
|
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.
|
4
|
+
version: 0.1.0
|
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-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -24,8 +24,8 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3.2'
|
27
|
-
description:
|
28
|
-
|
27
|
+
description: Collects tasks to be run, then execute in parallel, with maximum specified
|
28
|
+
concurrency.
|
29
29
|
email: fabio.ornellas@gmail.com
|
30
30
|
executables: []
|
31
31
|
extensions: []
|