You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm newbie for Ruby and I'm trying coroutine like something using Async and Ractor::Port.
# frozen_string_literal: true
require "async"
def f
Async do
n = Time.now
Async do
puts "async 1 before #{Time.now - n}"
sleep(0.5)
puts "async 1 after #{Time.now - n}"
end
Async do
puts "async 2 before #{Time.now - n}"
sleep(1)
puts "async 2 after #{Time.now - n}"
end
end
end
def g
port = Ractor::Port.new
Ractor.new port do |port|
Async do
sleep(1)
port.send(:message)
end
end
Async do
n = Time.now
Async do
puts "async 1 before #{Time.now - n}"
sleep(0.5)
puts "async 1 after #{Time.now - n}"
end
Async do
puts "async 2 before #{Time.now - n}"
port.receive
puts "async 2 after #{Time.now - n}"
end
end
end
~/.../ruby/my_gem_r4 $
and output is
[85] pry(main)> f
async 1 before 5.5424e-05
async 2 before 0.00047027
async 1 after 0.501706423
async 2 after 1.000711154
=> #<Async::Task:0x0000000000000688>
[86] pry(main)> g
async 1 before 0.000104577
async 2 before 0.000488231
async 2 after 1.001955307
async 1 after 1.002924576
=> #<Async::Task:0x0000000000000690>
[87] pry(main)>
But, I expected another result like below
[85] pry(main)> f
async 1 before 5.5424e-05
async 2 before 0.00047027
async 1 after 0.501706423
async 2 after 1.000711154
=> #<Async::Task:0x0000000000000688>
[86] pry(main)> g
async 1 before 0.000104577
async 2 before 0.000488231
async 1 after 0.502034231
async 2 after 1.002924576
=> #<Async::Task:0x0000000000000690>
[87] pry(main)>
That part that I think as a problem is output async 1 after's time difference.
Run port.receive on a separate thread, for instance via Thread.new { port.receive }.value or Fiber.current_scheduler.blocking_operation_wait(-> { port.receive } (untested)
Use Farce::Port instead (tested but not yet used in production).
Activity
rkh commentedon Oct 18, 2026
This is caused by https://bugs.ruby-lang.org/issues/22211
Working around it in async/io-event would require significant work.
There are a couple of workarounds:
Thread::QueueorAsync::Queueinstead.port.receiveon a separate thread, for instance viaThread.new { port.receive }.valueorFiber.current_scheduler.blocking_operation_wait(-> { port.receive }(untested)Farce::Portinstead (tested but not yet used in production).