Sha256: beb10532dc8f57ea19c156656aeba1b42e8efe59bc838bd6da25c9673d54cf69

Contents?: true

Size: 1.52 KB

Versions: 3

Compression:

Stored size: 1.52 KB

Contents

# -*- ruby -*-

require 'yugabyte_ysql'

$stdout.sync = true

# This is a example of how to mix and match synchronous and async APIs. In this case,
# the connection to the server is made syncrhonously, and then queries are
# asynchronous.

TIMEOUT = 5.0 # seconds to wait for an async operation to complete
CONN_OPTS = {
	:host     => 'localhost',
	:dbname   => 'test',
}

# Output progress messages
def output_progress( msg )
	puts ">>> #{msg}\n"
end

# Start the (synchronous) connection
output_progress "Starting connection..."
conn = YugabyteYSQL.connect(CONN_OPTS ) or abort "Unable to create a new connection!"

abort "Connect failed: %s" % [ conn.error_message ] unless conn.status == YugabyteYSQL::CONNECTION_OK

# Now grab a reference to the underlying socket to select() on while the query is running
socket = conn.socket_io

# Send the (asynchronous) query
output_progress "Sending query"
conn.send_query( "SELECT * FROM pg_stat_activity" )

# Fetch results until there aren't any more
loop do
	output_progress "  waiting for a response"

	# Buffer any incoming data on the socket until a full result is ready.
	conn.consume_input
	while conn.is_busy
		output_progress "  waiting for data to be available on %p..." % [ socket ]
		select( [socket], nil, nil, TIMEOUT ) or
			raise "Timeout waiting for query response."
		conn.consume_input
	end

	# Fetch the next result. If there isn't one, the query is finished
	result = conn.get_result or break

	output_progress "Query result:\n%p\n" % [ result.values ]
end

output_progress "Done."
conn.finish

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
yugabyte_ysql-0.3 sample/async_mixed.rb
yugabyte_ysql-0.2 sample/async_mixed.rb
yugabyte_ysql-0.1 sample/async_mixed.rb