Sha256: f7d080dcf1c0add7dd23b51bf2838843a76afdbee76d0cda2ba3b2554ba3ab15

Contents?: true

Size: 860 Bytes

Versions: 7

Compression:

Stored size: 860 Bytes

Contents

# frozen_string_literal: true

module SolargraphTestCoverage
  # https://stackoverflow.com/questions/1076257/returning-data-from-forked-processes
  # When called with a block, runs the content of said block in a new (forked) process
  # the return value of the process/block can be captured and used in parent process
  class ForkProcess
    #
    # Executes block in forked process, and captures returned value of that block
    # Returns result of block
    #
    # @return [Object]
    #
    def self.run
      read, write = IO.pipe

      pid = fork do
        read.close
        result = yield
        Marshal.dump(result, write)
        exit!(0) # skips exit handlers.
      end

      write.close
      result = read.read
      Process.wait(pid)
      raise ChildFailedError if result.nil? || result.empty?

      Marshal.load(result)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
solargraph_test_coverage-0.2.4 lib/solargraph_test_coverage/fork_process.rb
solargraph_test_coverage-0.2.3 lib/solargraph_test_coverage/fork_process.rb
solargraph_test_coverage-0.2.2 lib/solargraph_test_coverage/fork_process.rb
solargraph_test_coverage-0.2.1 lib/solargraph_test_coverage/fork_process.rb
solargraph_test_coverage-0.2.0 lib/solargraph_test_coverage/fork_process.rb
solargraph_test_coverage-0.1.2 lib/solargraph_test_coverage/fork_process.rb
solargraph_test_coverage-0.1.1 lib/solargraph_test_coverage/fork_process.rb