File: with_break.rb

package info (click to toggle)
ruby-parallel 1.22.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 464 kB
  • sloc: ruby: 1,930; makefile: 4
file content (29 lines) | stat: -rw-r--r-- 846 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# frozen_string_literal: true
require './spec/cases/helper'
$stdout.sync = true # otherwise results can go weird...

method = ENV.fetch('METHOD')
in_worker_type = "in_#{ENV.fetch('WORKER_TYPE')}".to_sym
worker_size = (ENV['WORKER_SIZE'] || 4).to_i

ARGV.freeze # make ractor happy

class Callback
  def self.call(x)
    $stdout.sync = true
    sleep 0.1 # so all workers get started
    print x
    raise Parallel::Break, *ARGV if x == 1
    sleep 0.2 # so now no work gets queued before Parallel::Break is raised
    x
  end
end

options = { in_worker_type => worker_size }
result =
  if in_worker_type == :in_ractors
    Parallel.public_send(method, 1..10, options.merge(ractor: [Callback, :call]))
  else
    Parallel.public_send(method, 1..10, options) { |x| Callback.call x }
  end
print " Parallel::Break raised - result #{result.inspect}"