Skip to content

Commit 2217b1a

Browse files
committed
Add support for Fiber::Scheduler#blocking_operation_wait.
1 parent b6bfc3c commit 2217b1a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

lib/async/scheduler.rb

+19
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,25 @@ def process_wait(pid, flags)
338338
return @selector.process_wait(Fiber.current, pid, flags)
339339
end
340340

341+
# Wait for the given work to be executed.
342+
#
343+
# @public Since *Async v2.19* and *Ruby v3.4*.
344+
# @asynchronous May be non-blocking.
345+
#
346+
# @parameter work [Proc] The work to execute on a background thread.
347+
# @returns [Object] The result of the work.
348+
def blocking_operation_wait(work)
349+
thread = Thread.new(&work)
350+
351+
result = thread.join
352+
353+
thread = nil
354+
355+
return result
356+
ensure
357+
thread&.kill
358+
end
359+
341360
# Run one iteration of the event loop.
342361
#
343362
# When terminating the event loop, we already know we are finished. So we don't need to check the task tree. This is a logical requirement because `run_once` ignores transient tasks. For example, a single top level transient task is not enough to keep the reactor running, but during termination we must still process it in order to terminate child tasks.

0 commit comments

Comments
 (0)