vnews 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,7 +20,7 @@ Vnews is free and open-source.
20
20
  ## Prerequisites
21
21
 
22
22
  * a recent version of Vim (Vnews is developed against Vim 7.2)
23
- * a recent version of Ruby: Ruby 1.9.2 is recommended
23
+ * Ruby 1.9: Ruby 1.9.2 is recommended
24
24
  * MySQL
25
25
  * the Unix program `tidy`
26
26
  * the Unix program `fmt`
@@ -0,0 +1,87 @@
1
+ # taken from http://snippets.dzone.com/posts/show/3276
2
+ require 'thread'
3
+
4
+ class ThreadPool
5
+ class Worker
6
+ def initialize
7
+ @mutex = Mutex.new
8
+ @thread = Thread.new do
9
+ while true
10
+ sleep 0.001
11
+ block = get_block
12
+ if block
13
+ block.call
14
+ reset_block
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ def get_block
21
+ @mutex.synchronize {@block}
22
+ end
23
+
24
+ def set_block(block)
25
+ @mutex.synchronize do
26
+ raise RuntimeError, "Thread already busy." if @block
27
+ @block = block
28
+ end
29
+ end
30
+
31
+ def reset_block
32
+ @mutex.synchronize {@block = nil}
33
+ end
34
+
35
+ def busy?
36
+ @mutex.synchronize {!@block.nil?}
37
+ end
38
+ end
39
+
40
+ attr_accessor :max_size
41
+ attr_reader :workers
42
+
43
+ def initialize(max_size = 10)
44
+ @max_size = max_size
45
+ @workers = []
46
+ @mutex = Mutex.new
47
+ end
48
+
49
+ def size
50
+ @mutex.synchronize {@workers.size}
51
+ end
52
+
53
+ def busy?
54
+ @mutex.synchronize {@workers.any? {|w| w.busy?}}
55
+ end
56
+
57
+ def join
58
+ sleep 0.01 while busy?
59
+ end
60
+
61
+ def process(&block)
62
+ wait_for_worker.set_block(block)
63
+ end
64
+
65
+ def wait_for_worker
66
+ while true
67
+ worker = find_available_worker
68
+ return worker if worker
69
+ sleep 0.01
70
+ end
71
+ end
72
+
73
+ def find_available_worker
74
+ @mutex.synchronize {free_worker || create_worker}
75
+ end
76
+
77
+ def free_worker
78
+ @workers.each {|w| return w unless w.busy?}; nil
79
+ end
80
+
81
+ def create_worker
82
+ return nil if @workers.size >= @max_size
83
+ worker = Worker.new
84
+ @workers << worker
85
+ worker
86
+ end
87
+ end
@@ -1,5 +1,6 @@
1
1
  require 'vnews/sql'
2
2
  require 'yaml'
3
+ require 'thread_pool'
3
4
 
4
5
  class Vnews
5
6
  def self.sql_client
@@ -85,13 +86,14 @@ class Vnews
85
86
  puts "Adding feeds: #{(new_feeds - old_feeds).inspect}"
86
87
  puts "Adding folder-feed associations: #{(ff - old_ff).inspect}"
87
88
  feeds2 = []
88
- threads = []
89
+ pool = ThreadPool.new(10)
90
+ puts "Using thread pool size of 10"
89
91
  ff.each do |feed_url, folder|
90
- threads << Thread.new do
92
+ pool.process do
91
93
  feeds2 << Vnews::Feed.fetch_feed(feed_url, folder)
92
94
  end
93
95
  end
94
- threads.each {|t| t.join}
96
+ pool.join
95
97
  feeds2.each do |x|
96
98
  feed_url, f, folder = *x
97
99
  folder ||= "Misc"
@@ -1,4 +1,5 @@
1
1
  require 'vnews/feed'
2
+ require 'thread_pool'
2
3
 
3
4
  class Vnews
4
5
  class Folder
@@ -10,14 +11,16 @@ class Vnews
10
11
  require 'vnews/display'
11
12
  folder = Vnews::Display.strip_item_count(folder)
12
13
  puts "Updating folder: #{folder.inspect}"
13
- threads = []
14
14
  feeds = []
15
+ pool = ThreadPool.new(10)
16
+ puts "Using thread pool size of 10"
15
17
  Vnews.sql_client.feeds_in_folder(folder.strip).each do |feed|
16
- threads << Thread.new do
18
+ pool.process do
19
+ sleep(rand(10))
17
20
  feeds << Vnews::Feed.fetch_feed(feed, folder)
18
21
  end
19
22
  end
20
- threads.each {|t| t.join}
23
+ pool.join
21
24
  puts "Saving data to database"
22
25
  feeds.select {|x| x[1]}.compact.each do |feed_url, f, folder|
23
26
  Vnews::Feed.save_feed feed_url, f, folder
@@ -11,9 +11,10 @@ class Vnews
11
11
  doc = Nokogiri::XML.parse(opml)
12
12
  feeds = []
13
13
  doc.xpath('/opml/body/outline').each_slice(CONCURRENCY) do |xs|
14
- threads = []
14
+ pool = ThreadPool.new(10)
15
+ puts "Using thread pool size of 10"
15
16
  xs.each do |n|
16
- threads << Thread.new do
17
+ pool.process do
17
18
  if n.attributes['xmlUrl']
18
19
  feeds << Vnews::Feed.fetch_feed(n.attributes['xmlUrl'].to_s)
19
20
  else
@@ -25,7 +26,7 @@ class Vnews
25
26
  end
26
27
  end
27
28
  end
28
- threads.each {|t| t.join}
29
+ pool.join
29
30
  end
30
31
 
31
32
  $stderr.puts "Making database records"
@@ -1,4 +1,4 @@
1
1
  class Vnews
2
- VERSION = '0.2.3'
2
+ VERSION = '0.2.4'
3
3
  end
4
4
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 3
9
- version: 0.2.3
8
+ - 4
9
+ version: 0.2.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Daniel Choi
@@ -78,6 +78,7 @@ files:
78
78
  - bin/vnews
79
79
  - bin/vnews-client
80
80
  - lib/create.sql
81
+ - lib/thread_pool.rb
81
82
  - lib/vnews.rb
82
83
  - lib/vnews.vim
83
84
  - lib/vnews/autodiscoverer.rb