Class: Fractor::WrappedRactor
- Inherits:
-
Object
- Object
- Fractor::WrappedRactor
- Defined in:
- lib/fractor/wrapped_ractor.rb
Overview
Base class for wrapped Ractors with shared functionality. Subclasses implement Ruby 3.x and Ruby 4.0+ specific communication patterns.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#ractor ⇒ Object
readonly
Returns the value of attribute ractor.
-
#worker_class ⇒ Object
readonly
Returns the value of attribute worker_class.
Class Method Summary collapse
-
.create(name, worker_class, **kwargs) ⇒ WrappedRactor
Factory method to create the appropriate WrappedRactor implementation based on the current Ruby version.
Instance Method Summary collapse
-
#close ⇒ Boolean
Closes the Ractor.
-
#closed? ⇒ Boolean
Checks if the Ractor is closed or unavailable.
-
#initialize(name, worker_class) ⇒ WrappedRactor
constructor
Initializes the WrappedRactor with a name and the Worker class.
-
#receive_message ⇒ Hash?
Receives a message from the Ractor.
-
#send(work) ⇒ Boolean
Sends work to the Ractor.
-
#start ⇒ Object
Starts the underlying Ractor.
Constructor Details
#initialize(name, worker_class) ⇒ WrappedRactor
Initializes the WrappedRactor with a name and the Worker class.
28 29 30 31 32 33 |
# File 'lib/fractor/wrapped_ractor.rb', line 28 def initialize(name, worker_class) puts "Creating Ractor #{name} with worker #{worker_class}" if ENV["FRACTOR_DEBUG"] @name = name @worker_class = worker_class @ractor = nil end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/fractor/wrapped_ractor.rb', line 7 def name @name end |
#ractor ⇒ Object (readonly)
Returns the value of attribute ractor.
7 8 9 |
# File 'lib/fractor/wrapped_ractor.rb', line 7 def ractor @ractor end |
#worker_class ⇒ Object (readonly)
Returns the value of attribute worker_class.
7 8 9 |
# File 'lib/fractor/wrapped_ractor.rb', line 7 def worker_class @worker_class end |
Class Method Details
.create(name, worker_class, **kwargs) ⇒ WrappedRactor
Factory method to create the appropriate WrappedRactor implementation based on the current Ruby version.
16 17 18 19 20 21 22 |
# File 'lib/fractor/wrapped_ractor.rb', line 16 def self.create(name, worker_class, **kwargs) if Fractor::RUBY_4_0_OR_HIGHER WrappedRactor4.new(name, worker_class, **kwargs) else WrappedRactor3.new(name, worker_class, **kwargs) end end |
Instance Method Details
#close ⇒ Boolean
Closes the Ractor.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/fractor/wrapped_ractor.rb', line 58 def close return true if @ractor.nil? begin # Send a nil message to signal we're done begin @ractor.send(nil) rescue StandardError nil end # Mark as closed in our object old_ractor = @ractor @ractor = nil # If available in this Ruby version, we'll try kill if old_ractor.respond_to?(:kill) begin old_ractor.kill rescue StandardError nil end end true rescue Exception => e puts "Warning: Error closing Ractor #{@name}: #{e.}" if ENV["FRACTOR_DEBUG"] # Consider it closed even if there was an error @ractor = nil true end end |
#closed? ⇒ Boolean
Checks if the Ractor is closed or unavailable. Uses a timeout to avoid blocking on Windows Ruby 3.4 where Ractor#inspect can block if the ractor is waiting on receive.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/fractor/wrapped_ractor.rb', line 96 def closed? return true if @ractor.nil? # Use a timeout to avoid blocking indefinitely on Windows Ruby 3.4 result = Timeout.timeout(0.1) do @ractor.inspect rescue Timeout::Error # Timeout means ractor is still running (not terminated) "#<Ractor:blocked>" end if result.include?("terminated") # If terminated, clean up our reference @ractor = nil return true end false rescue Exception # If we get an exception, the Ractor is likely terminated @ractor = nil true end |
#receive_message ⇒ Hash?
Receives a message from the Ractor. Must be implemented by subclasses.
51 52 53 |
# File 'lib/fractor/wrapped_ractor.rb', line 51 def raise NotImplementedError, "Subclasses must implement #receive_message" end |
#send(work) ⇒ Boolean
Sends work to the Ractor. Must be implemented by subclasses.
44 45 46 |
# File 'lib/fractor/wrapped_ractor.rb', line 44 def send(work) raise NotImplementedError, "Subclasses must implement #send" end |
#start ⇒ Object
Starts the underlying Ractor. Must be implemented by subclasses.
36 37 38 |
# File 'lib/fractor/wrapped_ractor.rb', line 36 def start raise NotImplementedError, "Subclasses must implement #start" end |