-
-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow running an Async block inside a Ractor #128
Comments
One potential solution would be to allow the injection of a sharable duplicate of ENV like so: Ractor.new(ENV) { |env| Async(env) { 42 } } If you like that solution, I would be happy to put together a pull request. |
There are lots of places where To be frank, I agree with your position that we should avoid using If we were to offer an alternative model, it would be something which still provided the same set of hooks but did so in a way which was Ractor compatible. For example:
We could do this at load time rather than lazy initialisation, but the down side to that is extra overhead at load time - maybe not a bad trade off. Then, we can define |
What about something like this: #!/usr/bin/env ruby
module Kernel
def env_accessor(name, key:, default: nil, &block)
self.singleton_class.attr_accessor(name)
if value = ENV[key]
value = yield(value) if block_given?
self.send(:"#{name}=", value)
else
self.send(:"#{name}=", default)
end
end
end
module Console
env_accessor :output, key: 'CONSOLE_OUTPUT' do |value|
value.split(',')
end
end
pp Console.output It resolves the environment variables at load time which should be more predictable. The only missing piece here is making the value immutable, of which the closest thing we have is |
Very nice. Don't think you need the |
I believe |
Using Ruby 3.0.2 I'm trying the following in Async 1.30.1 and 2.0.0:
This is the distilled means of reproducing the issue, and the larger context is that I wish to run two Fiber schedulers in parallel, one for handling input I/O and one for output.
For both mentioned versions you get an exception because you can't access
ENV
from inside the non-main ractor.Error message in 1.30.1 :
Error message in 2.0.0:
I can obviously work around the problem by using threads instead, and I'll also add that arguably this is more of a defect in the Ractor API, but on the other hand ENV is shared mutable state.
The text was updated successfully, but these errors were encountered: