~singpolyma/em_fiberscheduler

48534fa8da0bd1449348515f6579d4f53d3879c6 — Stephen Paul Weber 4 months ago 077f00e
Use EM.defer for ProcessWait fallback
1 files changed, 5 insertions(+), 19 deletions(-)

M lib/em_fiberscheduler.rb
M lib/em_fiberscheduler.rb => lib/em_fiberscheduler.rb +5 -19
@@ 130,33 130,19 @@ module EventMachine

			def call; end

			# Inspired by bruno-/fiber_scheduler
			class Fallback
				def initialize(fiber, pid, flags)
					@fiber = fiber
					@pid = pid
					@flags = flags
					@reader, @writer = IO.pipe
				end

				def thread
					@thread ||= Thread.new do
						Process::Status.wait(@pid, @flags)
					ensure
						@writer.close
					end
				end

				def call
					thread
					Fiber.schedule do
						Fiber.scheduler&.io_wait(@reader, IO::READABLE)
						@fiber.resume(thread.value)
					ensure
						@reader.close
						@writer.close
						@thread&.kill
					end
					EM.defer(
						-> { Process::Status.wait(@pid, @flags) },
						@fiber.method(:resume),
						@fiber.method(:raise)
					)
				end
			end
		end