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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/parallel_tasker.rb +24 -15
  3. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0d8fe4c994ffe659c9948b4d3730eae83f855f68
4
- data.tar.gz: 92e3a0c46fc26fd7d70f1c2ef8b22ad8da806980
3
+ metadata.gz: f4d9dc4e614c5092e3e3fb9f9ab98bd4a0e295d4
4
+ data.tar.gz: 0c78457e01b3501c4f438696c7894427e14635de
5
5
  SHA512:
6
- metadata.gz: 356927b1f3c8491cb77276bb837f420fa67bfdf816c63695d1c7b461beb8857626bebfb40e2a3945de1ee6b0e9cfb5503cbf0df35e84712b826a4c1bca588818
7
- data.tar.gz: 989da5fe6193e68cefb4d08a783e0d952b1c6b442fb18f5c749e799c0fbaf319426e465dcdc4b03852bae858aa68464e3fcefe7e984ffbb245df0e7cd3be8628
6
+ metadata.gz: ed83725b491608eba9e48f2b81ba2ca838a0ba9e07357496f3fcaf4a1dbedfa1c33b9ae65857493006f754aa15ed4530751387c8f1f3b75d2e9efc3d3e4e2e2b
7
+ data.tar.gz: 790488766ebfe04a839f02f67390e007d5e763177f495d817211acd4ef71a43a539482cc4b0073069b8d851bc3579bb46d48951d0a66a252e709f96e2def9b3c
@@ -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
- def initialize limit
8
- @limit = limit
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 limit of parallel
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 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.
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
- @limit = @tasks.size if @limit > @tasks.size
41
+ @concurrency = @tasks.size if @concurrency > @tasks.size
33
42
  pending_ids = @tasks.keys
34
- @running_ids = []
43
+ @already_running_ids = []
35
44
  completed_ids = []
36
45
  # start initial batch
37
- pending_ids.shift(@limit).each{|id| new_thread(id)}
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
- @running_ids.delete completed_id
52
+ @already_running_ids.delete completed_id
44
53
  completed_ids << completed_id
45
- # start new thread if available and below limit
46
- if not pending_ids.empty? and @running_ids.size < @limit
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
- @running_ids << id
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
- @running_ids.each do |id|
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.0.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-27 00:00:00.000000000 Z
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: Simple Gem that collects tasks to be run, then execute them with maximum
28
- specified parallelism.
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: []