ztimer 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +10 -7
  3. data/lib/ztimer/version.rb +1 -1
  4. data/lib/ztimer.rb +47 -12
  5. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5e403be0894d09cc0bbc6c390b101665532e8a18
4
- data.tar.gz: eb034343767cadf543893013f0e115319f51b2ed
3
+ metadata.gz: 1e9203fd984c0ca9b608ec0a473f7653eee3a943
4
+ data.tar.gz: 8c998b3131d0a6c969bdac9b994fcaf9beeefc32
5
5
  SHA512:
6
- metadata.gz: f80e696e261cdc59c62e1e4daae92d0734dd432130f8c18cdd80f7f2b701c0ec945344f12ce390ff8e797c900812ca6d31b9fe91e4fce81d3301404f0a3407ed
7
- data.tar.gz: 786b8c931a6775197fb0475f92f5a3a223fb7845ed3a2f6a85faa920413ead1b32366424ddb80964c15152f367d53ccbaa15ecfbef01bdfee212db51cbf527b5
6
+ metadata.gz: 10b8f4fb11edd3746d143456d89fa85e57cf0d46c0166ed15e6a678d240fcdc1fbafbe19a5e2c130972b66988a54ad13f1b58b00fef74ad9ee01a5a7b132ef65
7
+ data.tar.gz: 02e53d5d6f47657543503afac5a5ea29de347c78a039673900a9da2e10fac7952b1d1f02c956f1e70e66a104769204207711474d556c15f3ed1470ce595af8bd
data/README.md CHANGED
@@ -32,6 +32,12 @@ Ztimer.after(delay) do
32
32
  puts "Doing something useful..."
33
33
  end
34
34
 
35
+ # Async execution
36
+ Ztimer.async do
37
+ # this code will be executed in background asyncronously
38
+ puts "Doing something useful in background..."
39
+ end
40
+
35
41
  # Recurrent jobs
36
42
  job = Ztimer.every(delay) do # execute the block every second
37
43
  puts "Executing a recurrent job..."
@@ -42,15 +48,12 @@ job.cancel! # cancel the recurrent job
42
48
 
43
49
  ```
44
50
 
45
- By default **Ztimer** will run at maximum 20 notifications concurrently, so that if you have 100 notifications to be
46
- executed at the same time, at maximum 20 of them will run at the same time. This is necessary in order to prevent uncontrolled
47
- threads spawn when many notifications have to be sent at the same time.
51
+ By default **Ztimer** will run at maximum 20 jobs concurrently, so that if you have 100 jobs to be
52
+ executed at the same time, at maximum 20 of them will run at the same time. This is necessary in order to prevent uncontrolled threads spawn when many jobs have to be sent at the same time.
48
53
 
49
- Anyway, you can change the concurrency by calling `Ztimer.concurrency = <concurrency>`, where `<concurrency>` is the maximum number
50
- of `Ztimer` workers allowed to run in parallel (ex: `Ztimer.concurrency = 50`).
54
+ Anyway, you can change the concurrency by calling `Ztimer.concurrency = <concurrency>`, where `<concurrency>` is the maximum number of `Ztimer` workers allowed to run in parallel (ex: `Ztimer.concurrency = 50`).
51
55
 
52
56
  ## Contributing
53
57
 
54
- Bug reports and pull requests are welcome on GitHub at https://github.com/serioja90/ztimer. This project is intended to be a safe,
55
- welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/serioja90/ztimer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
56
59
 
@@ -1,3 +1,3 @@
1
1
  module Ztimer
2
- VERSION = "0.4.3"
2
+ VERSION = "0.5.0"
3
3
  end
data/lib/ztimer.rb CHANGED
@@ -7,12 +7,23 @@ module Ztimer
7
7
  @concurrency = 20
8
8
  @watcher = Ztimer::Watcher.new(){|slot| execute(slot) }
9
9
  @workers_lock = Mutex.new
10
+ @count_lock = Mutex.new
10
11
  @queue = Queue.new
11
12
  @running = 0
12
13
  @count = 0
13
14
 
14
15
  class << self
15
- attr_reader :concurrency, :running, :count
16
+ attr_reader :concurrency, :running, :count, :watcher, :queue
17
+
18
+ def async(&callback)
19
+ enqueued_at = utc_microseconds
20
+ slot = Slot.new(enqueued_at, enqueued_at, -1, &callback)
21
+
22
+ incr_counter!
23
+ execute(slot)
24
+
25
+ return slot
26
+ end
16
27
 
17
28
  def after(milliseconds, &callback)
18
29
  enqueued_at = utc_microseconds
@@ -43,13 +54,26 @@ module Ztimer
43
54
  @concurrency = new_value
44
55
  end
45
56
 
57
+
58
+ def stats
59
+ {
60
+ running: @running,
61
+ scheduled: @watcher.jobs,
62
+ executing: @queue.size,
63
+ total: @count
64
+ }
65
+ end
66
+
46
67
  protected
47
68
 
48
69
  def add(slot)
49
- @count += 1
70
+ incr_counter!
50
71
  @watcher << slot
51
72
  end
52
73
 
74
+ def incr_counter!
75
+ @count_lock.synchronize{ @count += 1 }
76
+ end
53
77
 
54
78
  def execute(slot)
55
79
  @queue << slot
@@ -57,23 +81,34 @@ module Ztimer
57
81
  @workers_lock.synchronize do
58
82
  [@concurrency - @running, @queue.size].min.times do
59
83
  @running += 1
60
- worker = Thread.new do
84
+ start_new_thread!
85
+ end
86
+ end
87
+ end
88
+
89
+ def start_new_thread!
90
+ worker = Thread.new do
91
+ begin
92
+ loop do
93
+ current_slot = nil
94
+ @workers_lock.synchronize do
95
+ current_slot = @queue.pop(true) unless @queue.empty?
96
+ end
97
+ break if current_slot.nil?
98
+
61
99
  begin
62
- while !@queue.empty? && (slot = @queue.pop(true)) do
63
- slot.executed_at = utc_microseconds
64
- slot.callback.call(slot) unless slot.callback.nil?
65
- end
66
- rescue ThreadError
67
- # queue is empty
68
- puts "queue is empty"
100
+ current_slot.executed_at = utc_microseconds
101
+ current_slot.callback.call(current_slot) unless current_slot.callback.nil? || current_slot.canceled?
69
102
  rescue => e
70
103
  STDERR.puts e.inspect + (e.backtrace ? "\n" + e.backtrace.join("\n") : "")
71
104
  end
72
- @workers_lock.synchronize { @running -= 1 }
73
105
  end
74
- worker.abort_on_exception = true
106
+ rescue ThreadError
107
+ puts "queue is empty"
75
108
  end
109
+ @workers_lock.synchronize { @running -= 1 }
76
110
  end
111
+ worker.abort_on_exception = true
77
112
  end
78
113
 
79
114
  def utc_microseconds
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ztimer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Groza Sergiu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-20 00:00:00.000000000 Z
11
+ date: 2016-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  version: '0'
96
96
  requirements: []
97
97
  rubyforge_project:
98
- rubygems_version: 2.4.6
98
+ rubygems_version: 2.4.8
99
99
  signing_key:
100
100
  specification_version: 4
101
101
  summary: An asyncrhonous timer