dispatch_queue 1.0.5 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +9 -0
- data/README.md +24 -25
- data/lib/dispatch_queue.rb +35 -1
- data/test/test_dispatch_queue.rb +15 -0
- metadata +5 -4
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -4,60 +4,59 @@ DispatchQueue
|
|
4
4
|
DispatchQueue is a simple way to serialize jobs.
|
5
5
|
It was done for crawling things, but could be used in any case that you need to do more than one work at a time.
|
6
6
|
|
7
|
-
With this line, we get more documentation that real code, so please see the code.
|
8
|
-
|
9
|
-
[![Build Status](https://secure.travis-ci.org/guillermo/dispatch_queue.png)](http://travis-ci.org/guillermo/dispatch_queue)
|
10
7
|
|
11
8
|
|
12
9
|
COMPATIBILITY
|
13
10
|
=============
|
14
11
|
|
15
|
-
|
12
|
+
Check build status to see support in different platforms
|
13
|
+
|
14
|
+
[![Build Status](https://secure.travis-ci.org/guillermo/dispatch_queue.png)](http://travis-ci.org/guillermo/dispatch_queue)
|
16
15
|
|
17
16
|
USAGE
|
18
17
|
=====
|
19
18
|
|
20
|
-
* The
|
19
|
+
* The threaded_way:
|
21
20
|
|
22
21
|
```ruby
|
23
|
-
|
24
|
-
|
22
|
+
|
23
|
+
[1,2,3].threaded_each{|i|
|
24
|
+
sleep 4-i
|
25
|
+
puts i
|
26
|
+
}
|
27
|
+
|
25
28
|
```
|
26
29
|
|
27
|
-
|
30
|
+
3
|
31
|
+
2
|
32
|
+
1
|
28
33
|
|
29
34
|
```ruby
|
30
|
-
|
35
|
+
[1,2,3].threaded_map{|i| sleep 4-i ; i } #=> [3,2,1]
|
31
36
|
```
|
32
37
|
|
33
|
-
|
34
|
-
REALCODE
|
35
|
-
=======
|
38
|
+
* The explicit way:
|
36
39
|
|
37
40
|
```ruby
|
41
|
+
my_work_queue = DispatchQueue.new(lambda{ sleep 3 ; 5}, lambda{ 10 })
|
42
|
+
my_work_queue.sort #=> [5, 10]
|
43
|
+
```
|
38
44
|
|
39
|
-
|
40
|
-
VERSION = "1.0.5"
|
41
|
-
|
42
|
-
def self.[](*procs)
|
43
|
-
procs.map { |proc|
|
44
|
-
Thread.new{ proc.to_proc.call }
|
45
|
-
}.map { |thread|
|
46
|
-
thread.value
|
47
|
-
}
|
48
|
-
end
|
49
|
-
end
|
45
|
+
* The implicit way:
|
50
46
|
|
47
|
+
```ruby
|
48
|
+
DQ[ lambda{ sleep 5 ; 3} , Proc.new{ sleep 6; 2 } ].sort #=> [2,3]
|
49
|
+
```
|
51
50
|
|
52
|
-
DQ = DispatchQueue
|
53
51
|
|
54
|
-
```
|
55
52
|
|
56
53
|
CHANGELOG
|
57
54
|
=========
|
58
55
|
|
56
|
+
* 1.1.0 Create [].threaded_each [].threaded_map (2011/09/22)
|
59
57
|
* 1.0.5 Reduce implementation
|
60
58
|
|
59
|
+
|
61
60
|
LICENSE
|
62
61
|
=======
|
63
62
|
|
data/lib/dispatch_queue.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class DispatchQueue
|
2
|
-
VERSION = "1.0
|
2
|
+
VERSION = "1.1.0"
|
3
3
|
|
4
4
|
def self.[](*procs)
|
5
5
|
procs.map { |proc|
|
@@ -10,6 +10,40 @@ class DispatchQueue
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
+
module ArrayThreadedEnumerable
|
14
|
+
def threaded_each
|
15
|
+
threads = []
|
16
|
+
each { |value|
|
17
|
+
threads << Thread.new {
|
18
|
+
yield value
|
19
|
+
}
|
20
|
+
}
|
21
|
+
threads.each{|t| t.join}
|
22
|
+
end
|
23
|
+
|
24
|
+
def threaded_map
|
25
|
+
values = []
|
26
|
+
threaded_each { |value|
|
27
|
+
value = yield value
|
28
|
+
Thread.exclusive { values << value }
|
29
|
+
}
|
30
|
+
values
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
Array.send(:include, ArrayThreadedEnumerable)
|
13
35
|
|
14
36
|
DQ = DispatchQueue
|
15
37
|
|
38
|
+
if RUBY_VERSION < "1.8.9999"
|
39
|
+
class Thread
|
40
|
+
def self.exclusive
|
41
|
+
old_value = Thread.critical
|
42
|
+
Thread.critical = true
|
43
|
+
yield
|
44
|
+
ensure
|
45
|
+
Thread.critical = old_value
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
data/test/test_dispatch_queue.rb
CHANGED
@@ -10,4 +10,19 @@ class DispatchQueueTest < Test::Unit::TestCase
|
|
10
10
|
assert_equal [3,4], DQ[lambda{ sleep 0.1; 4}, lambda{ 3}].sort
|
11
11
|
end
|
12
12
|
|
13
|
+
def test_threaded_each
|
14
|
+
results = []
|
15
|
+
|
16
|
+
[1,2,3].threaded_each { |value|
|
17
|
+
value *= 2
|
18
|
+
Thread.exclusive{ results << value }
|
19
|
+
}
|
20
|
+
|
21
|
+
assert_equal [2,4,6], results.sort
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_threaded_map
|
25
|
+
assert_equal [2,4,6], [1,2,3].threaded_map{|a| a*2}.sort
|
26
|
+
end
|
27
|
+
|
13
28
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dispatch_queue
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 1.0.5
|
10
|
+
version: 1.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- "Guillermo A\xCC\x81lvarez"
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-09-
|
18
|
+
date: 2011-09-21 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rake
|
@@ -42,6 +42,7 @@ extra_rdoc_files: []
|
|
42
42
|
|
43
43
|
files:
|
44
44
|
- .gitignore
|
45
|
+
- .travis.yml
|
45
46
|
- Gemfile
|
46
47
|
- README.md
|
47
48
|
- Rakefile
|