sql >> データベース >  >> RDS >> PostgreSQL

postgresqlgemasyncを使用する

    pg ライブラリは、PostgreSQLの非同期APIを完全にサポートします。 を追加しました samples/への使用方法の説明 ディレクトリ:

    #!/usr/bin/env ruby
    
    require 'pg'
    
    # This is a example of how to use the asynchronous API to query the
    # server without blocking other threads. It's intentionally low-level;
    # if you hooked up the PGconn#socket to some kind of reactor, you
    # could make this much nicer.
    
    TIMEOUT = 5.0 # seconds to wait for an async operation to complete
    CONN_OPTS = {
        :host     => 'localhost',
        :dbname   => 'test',
        :user     => 'jrandom',
        :password => 'banks!stealUR$',
    }
    
    # Print 'x' continuously to demonstrate that other threads aren't
    # blocked while waiting for the connection, for the query to be sent,
    # for results, etc. You might want to sleep inside the loop or 
    # comment this out entirely for cleaner output.
    progress_thread = Thread.new { loop { print 'x' } }
    
    # Output progress messages
    def output_progress( msg )
        puts "\n>>> #{msg}\n"
    end
    
    # Start the connection
    output_progress "Starting connection..."
    conn = PGconn.connect_start( CONN_OPTS ) or 
        abort "Unable to create a new connection!"
    abort "Connection failed: %s" % [ conn.error_message ] if
        conn.status == PGconn::CONNECTION_BAD
    
    # Now grab a reference to the underlying socket so we know when the
    # connection is established
    socket = IO.for_fd( conn.socket )
    
    # Track the progress of the connection, waiting for the socket to 
    # become readable/writable before polling it
    poll_status = PGconn::PGRES_POLLING_WRITING
    until poll_status == PGconn::PGRES_POLLING_OK ||
          poll_status == PGconn::PGRES_POLLING_FAILED
    
        # If the socket needs to read, wait 'til it becomes readable to
        # poll again
        case poll_status
        when PGconn::PGRES_POLLING_READING
            output_progress "  waiting for socket to become readable"
            select( [socket], nil, nil, TIMEOUT ) or
                raise "Asynchronous connection timed out!"
    
        # ...and the same for when the socket needs to write
        when PGconn::PGRES_POLLING_WRITING
            output_progress "  waiting for socket to become writable"
            select( nil, [socket], nil, TIMEOUT ) or
                raise "Asynchronous connection timed out!"
        end
    
        # Output a status message about the progress
        case conn.status
        when PGconn::CONNECTION_STARTED
            output_progress "  waiting for connection to be made."
        when PGconn::CONNECTION_MADE
            output_progress "  connection OK; waiting to send."
        when PGconn::CONNECTION_AWAITING_RESPONSE
            output_progress "  waiting for a response from the server."
        when PGconn::CONNECTION_AUTH_OK
            output_progress "  received authentication; waiting for " +
                            "backend start-up to finish."
        when PGconn::CONNECTION_SSL_STARTUP
            output_progress "  negotiating SSL encryption."
        when PGconn::CONNECTION_SETENV
            output_progress "  negotiating environment-driven " +
                            "parameter settings."
        end
    
        # Check to see if it's finished or failed yet
        poll_status = conn.connect_poll
    end
    
    abort "Connect failed: %s" % [ conn.error_message ] unless 
        conn.status == PGconn::CONNECTION_OK
    
    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
            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
    
        puts "\n\nQuery result:\n%p\n" % [ result.values ]
    end
    
    output_progress "Done."
    conn.finish
    
    if defined?( progress_thread )
        progress_thread.kill
        progress_thread.join
    end
    

    のドキュメントを読むことをお勧めします。 PQconnectStart 関数と非同期コマンド処理 PostgreSQLマニュアルのセクションを参照してから、上記のサンプルと比較してください。

    私はこれまでEventMachineを使用したことがありませんが、ソケットとコールバックを登録して読み取り/書き込み可能になったときに、データベース呼び出しを統合するのはかなり簡単だと思います。

    私はファイバーを使用してイベントコードをクリーンアップすることに関するIlyaGrigorikの記事 非同期APIを使いやすくするためですが、それは方法です。 チケットを持っています開く 自分でやる気がある/やる気がある場合は追跡します。



    1. Laravel移行プライマリ(またはキー)識別子名が長すぎます

    2. MariaDBでのTIMEDIFF()のしくみ

    3. hibernate + springアプリケーションからmysqlにcsvファイルをインポートする方法は?

    4. Dockerコンテナで実行されているIntellijからmySqlに接続できません-指定されたデータベースユーザー/パスワードの組み合わせは拒否されます