e-threadpool 1.0.2 → 1.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.
data/README.md CHANGED
@@ -27,7 +27,7 @@ class TestJob < Job
27
27
  end
28
28
  end
29
29
 
30
- threadpool = Threadpool::Threadpool.new
30
+ threadpool = Threadpool.new
31
31
  100.times.each do
32
32
  # threadpool auto-executes the job after loaded
33
33
  threadpool.load(TestJob.new)
@@ -17,7 +17,7 @@ module Ethreadpool
17
17
  end
18
18
 
19
19
  @workers = (0...@init_workers).map { Threadpool::Worker.new }
20
- Thread.new { process }
20
+ @checker_thread = Thread.new { run_checker }
21
21
  end
22
22
 
23
23
  def load(job)
@@ -30,6 +30,8 @@ module Ethreadpool
30
30
  loop do
31
31
  worker = idle_worker
32
32
  worker.nil? ? create_worker : break
33
+
34
+ # sleep here is a must, or MRI will get stucks
33
35
  sleep(0.001)
34
36
  end
35
37
 
@@ -38,26 +40,19 @@ module Ethreadpool
38
40
 
39
41
  def shutdown
40
42
  @teminate = true
41
-
42
- loop do
43
- return if busy_workers.count == 0
44
- sleep(0.001)
45
- end
43
+ @checker_thread.join
46
44
  end
47
45
 
48
46
  private
49
- def process
47
+ def run_checker
50
48
  loop do
51
49
  synchronize do
52
50
  @workers.each do |w|
53
-
54
- if w.loaded?
55
- w.process
56
-
57
- if Time.now - w.start_time > @timeout_secs
58
- puts "job is timeout."
59
- w.cancel
60
- end
51
+ w.process
52
+ t = w.start_time
53
+ if t && (Time.now - t > @timeout_secs)
54
+ puts "job #{w.jobid} is timeout."
55
+ w.cancel
61
56
  end
62
57
 
63
58
  end
@@ -12,7 +12,7 @@ module Ethreadpool
12
12
  end
13
13
 
14
14
  def process
15
- return if @start_time
15
+ return if @start_time || @job.nil?
16
16
  @start_time = Time.now
17
17
  @thread = Thread.new {
18
18
  @job.run
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: e-threadpool
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: