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
Is it possible to easily take an existing fiber i got from a library and basically just turn it into a task so i can await it, or await it by it self?
Its obviously a "finite" fiber that fits into what an async task expects, aka it yields nil until it at some point yields something truthy and then closes
# This is a critical failure, we should stop the reactor:
raise
ensure
# Console.info(self) {"Task ensure $! = #{$!} with #{@children&.size.inspect} children!"}
finish!
end
Therefore, it's not possible to wrap an existing fiber as it won't emit those events.
However, fibers are pretty cheap and will get scheduled even if not wrapped in tasks:
Asyncdo |parent|
3.timesdoparent.asyncdoFiber.newdosleep1puts"#{Fiber.current} done."end.resumeendendend# All done at the same time:#<Fiber:0x00007fdd70f269d0 (irb):5 (resumed)> done.#<Fiber:0x00007fdd70f226a0 (irb):5 (resumed)> done.#<Fiber:0x00007fdd70f21ae8 (irb):5 (resumed)> done.# => #<Async::Task:0x0000000000002290>
So I suggest something like this:
Asyncdo# Resume it once:existing_fiber.resume# Or if it can yield values:whilevalue=existing_fiber.resumeprocess(value)end# Or if you just want to resume it until completion:whileexisting_fiber.alive?existing_fiber.resumeendend
should i throw a sleep in there somewhere? or some other way to tell the scheduler "hey this async task here, you can just schedule it at the end of whatever event queue you have"
Activity
ioquatix commentedon Nov 11, 2025
There are specific requirements around the life cycle of a fiber:
async/lib/async/task.rb
Lines 451 to 465 in a093f3a
Therefore, it's not possible to wrap an existing fiber as it won't emit those events.
However, fibers are pretty cheap and will get scheduled even if not wrapped in tasks:
So I suggest something like this:
Hope that helps!
cramt commentedon Nov 13, 2025
Thanks, but doesnt busy waiting like this block the async scheduler? if my fiber basically equivilant to
should i throw a sleep in there somewhere? or some other way to tell the scheduler "hey this async task here, you can just schedule it at the end of whatever event queue you have"