Skip to content

Instantly share code, notes, and snippets.

@arthurnn
Created May 17, 2016 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthurnn/a449f1aebe302fed29bea07b60276e0b to your computer and use it in GitHub Desktop.
Save arthurnn/a449f1aebe302fed29bea07b60276e0b to your computer and use it in GitHub Desktop.
require 'pg'
class Context
attr_reader :connections
def initialize
@connections = {}
end
def connection
@connections.fetch(Process.pid) { @connections[Process.pid] = PG.connect( dbname: 'test' ) }
end
end
context = Context.new
p context.connection.exec( "SELECT current_database();" ).to_a
parent_pid = Process.pid
fork do
at_exit do
IO.for_fd(context.connections[parent_pid].socket).close
end
end
Process.wait
p context.connection.exec( "SELECT current_database();" ).to_a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment