threaded_processor 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/threaded_processor.rb +47 -0
- metadata +55 -0
@@ -0,0 +1,47 @@
|
|
1
|
+
VERSION = 0.1
|
2
|
+
|
3
|
+
require 'thread'
|
4
|
+
|
5
|
+
class ThreadedProcessorError < StandardError; end
|
6
|
+
|
7
|
+
class ThreadedProcessor
|
8
|
+
attr_accessor :list, :max_threads, :block
|
9
|
+
|
10
|
+
# Provide an object list array, thread count (Integer),
|
11
|
+
# and a block to run
|
12
|
+
def initialize(object_list=[], thread_count=3, &block)
|
13
|
+
@list = object_list
|
14
|
+
@max_threads = thread_count
|
15
|
+
@queue = Queue.new
|
16
|
+
@block = block
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
# Append an item to the object list
|
21
|
+
def <<(obj)
|
22
|
+
@list << obj
|
23
|
+
end
|
24
|
+
|
25
|
+
# Empty the object list
|
26
|
+
def empty!
|
27
|
+
@list = []
|
28
|
+
end
|
29
|
+
|
30
|
+
# Run the multithreaded process.
|
31
|
+
def run!
|
32
|
+
raise ThreadedProcessorError.new("Object list is empty!") if (@list.empty? || !@list.is_a?(Array))
|
33
|
+
raise ThreadedProcessorError.new("Assign block") if @block.nil?
|
34
|
+
threads = (1..@max_threads).map do |i|
|
35
|
+
Thread.new(@queue) do |q|
|
36
|
+
until ( q == ( obj = q.deq ) )
|
37
|
+
@block.call(obj)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
@list.each{|o| @queue.enq o}
|
42
|
+
threads.size.times{@queue.enq @queue}
|
43
|
+
threads.each{|t| t.join}
|
44
|
+
true
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: threaded_processor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Patrick Morgan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-02-05 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A simple wrapper for Ruby Threads and SizedQueue.
|
17
|
+
email: patrick@patrick-morgan.net
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/threaded_processor.rb
|
26
|
+
has_rdoc: true
|
27
|
+
homepage: http://www.patrick-morgan.net
|
28
|
+
licenses: []
|
29
|
+
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: "0"
|
40
|
+
version:
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.3.5
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: Threaded processor with specifiable thread pool size.
|
54
|
+
test_files: []
|
55
|
+
|