async 1.30.2 → 1.31.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 505786d92573cb06d44ba76fdf587c1a8d5ac1eab7e373fbb5380f85ada6686e
4
- data.tar.gz: bc935c07dcbe370ff7379971e670548be2133891e3f4ba703e434e109d691283
3
+ metadata.gz: f54f8b2dedcd29cf6a5d5c19b9b6ef11762cac77156439930b29d414e45bb6d4
4
+ data.tar.gz: c420baf542fe9992737ac9d8594cd3f88bb7764904113d801aa891ec328558ee
5
5
  SHA512:
6
- metadata.gz: 479cb9a9fad1eeae60993051cd91d8698b47f7dc6fd0129b4672bedf8bcb572c0515d91f5a0d4872da3c2f7d72941c316d86b9d9784fcb2744b1c746c8ba7f20
7
- data.tar.gz: c954400df7d37eb5f85dda0365390eb77205a0e2293a3c43291136bf1abcdc522c7b5171e465fa4d1026e9d704a9a7b30c1a765633d0e4c5249b1973b3e7036e
6
+ metadata.gz: ff4f467f2d8f2bde924e29636d8d0689509acfd1d463825173d03aa75b4235847db43c7fc6a9d4a10e18a444d542332218e1f3a9a0bb471d1a918bb411dd7a73
7
+ data.tar.gz: 95affc48630a0b2e83457e58024dfa89c091060b1047151849e30bce3a493a5e5ffaa06e824086df347dfb862f66cf834a08f3609d4b718eb7e3c74157bc1ad9
data/lib/async/queue.rb CHANGED
@@ -42,14 +42,16 @@ module Async
42
42
  @items.empty?
43
43
  end
44
44
 
45
- def enqueue(item)
46
- @items.push(item)
45
+ def <<(item)
46
+ @items << item
47
47
 
48
48
  self.signal unless self.empty?
49
49
  end
50
50
 
51
- def <<(item)
52
- enqueue(item)
51
+ def enqueue(*items)
52
+ @items.concat(items)
53
+
54
+ self.signal unless self.empty?
53
55
  end
54
56
 
55
57
  def dequeue
@@ -89,7 +91,7 @@ module Async
89
91
  @items.size >= @limit
90
92
  end
91
93
 
92
- def enqueue item
94
+ def <<(item)
93
95
  while limited?
94
96
  @full.wait
95
97
  end
@@ -97,6 +99,19 @@ module Async
97
99
  super
98
100
  end
99
101
 
102
+ def enqueue *items
103
+ while !items.empty?
104
+ while limited?
105
+ @full.wait
106
+ end
107
+
108
+ available = @limit - @items.size
109
+ @items.concat(items.shift(available))
110
+
111
+ self.signal unless self.empty?
112
+ end
113
+ end
114
+
100
115
  def dequeue
101
116
  item = super
102
117
 
@@ -40,6 +40,25 @@ module Async
40
40
  # The tasks waiting on this semaphore.
41
41
  attr :waiting
42
42
 
43
+ # Allow setting the limit. This is useful for cases where the semaphore is used to limit the number of concurrent tasks, but the number of tasks is not known in advance or needs to be modified.
44
+ #
45
+ # On increasing the limit, some tasks may be immediately resumed. On decreasing the limit, some tasks may execute until the count is < than the limit.
46
+ #
47
+ # @parameter limit [Integer] The new limit.
48
+ def limit= limit
49
+ difference = limit - @limit
50
+ @limit = limit
51
+
52
+ # We can't suspend
53
+ if difference > 0
54
+ difference.times do
55
+ break unless fiber = @waiting.shift
56
+
57
+ fiber.resume
58
+ end
59
+ end
60
+ end
61
+
43
62
  # Is the semaphore currently acquired?
44
63
  def empty?
45
64
  @count.zero?
data/lib/async/task.rb CHANGED
@@ -230,18 +230,19 @@ module Async
230
230
  private
231
231
 
232
232
  # This is a very tricky aspect of tasks to get right. I've modelled it after `Thread` but it's slightly different in that the exception can propagate back up through the reactor. If the user writes code which raises an exception, that exception should always be visible, i.e. cause a failure. If it's not visible, such code fails silently and can be very difficult to debug.
233
- # As an explcit choice, the user can start a task which doesn't propagate exceptions. This only applies to `StandardError` and derived tasks. This allows tasks to internally capture their error state which is raised when invoking `Task#result` similar to how `Thread#join` works. This mode makes {ruby Async::Task} behave more like a promise, and you would need to ensure that someone calls `Task#result` otherwise you might miss important errors.
234
- def fail!(exception = nil, propagate = true)
233
+ def fail!(exception = false, propagate = true)
235
234
  @status = :failed
236
235
  @result = exception
237
236
 
238
- if propagate
239
- raise
240
- elsif @finished.nil?
241
- # If no one has called wait, we log this as an error:
242
- Console.logger.error(self) {$!}
243
- else
244
- Console.logger.debug(self) {$!}
237
+ if exception
238
+ if propagate
239
+ raise exception
240
+ elsif @finished.nil?
241
+ # If no one has called wait, we log this as a warning:
242
+ Console.logger.warn(self, "Task may have ended with unhandled exception.", exception)
243
+ else
244
+ Console.logger.debug(self, exception)
245
+ end
245
246
  end
246
247
  end
247
248
 
data/lib/async/version.rb CHANGED
@@ -21,5 +21,5 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  module Async
24
- VERSION = "1.30.2"
24
+ VERSION = "1.31.0"
25
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.30.2
4
+ version: 1.31.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-04 00:00:00.000000000 Z
11
+ date: 2023-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: console
@@ -180,7 +180,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
180
180
  - !ruby/object:Gem::Version
181
181
  version: '0'
182
182
  requirements: []
183
- rubygems_version: 3.1.6
183
+ rubygems_version: 3.4.7
184
184
  signing_key:
185
185
  specification_version: 4
186
186
  summary: A concurrency framework for Ruby.