5.12.Example - 行事駆動結果による非同期問合せ Asynchronous Query with Event-Driven Results
Prev | Chapter5.Example Programs | Next

## 5.12.Example - Asynchronous Query with Event-Driven Results

前の用例の様にこの用例も､ Section4.4, “Asynchronous Query Processing Commands”に記述された､非同期問合せ命令を使う｡ Section5.11, “Example - Asynchronous Queries” と違って､この用例では､結果が準備できた時､tcl解釈実行機能に通知する為に行事把持子を確立する為に､pg_result_callbackを使う｡ この特性は､ pgtcl-ng に 1.6.0版で追加された｡この用例中で､tclは､警告時刻到達や､問合せからの結果を待つ｡ 同じ方法が､ファイルやソケット行事待ち､或いは､tcl 対話機能内の使用者動作対応､又､問合せ結果待ちの間に使用可能である｡註意: これは完全な記述譜ではない｡ Like the previous example, this example uses the asynchronous query commands described in Section4.4, “Asynchronous Query Processing Commands”. Unlike that example in Section5.11, “Example - Asynchronous Queries”, this example uses pg_result_callback to establish an event handler to notify the Tcl interpreter when a result is ready. This feature was added to pgtcl-ng at version 1.6.0. In this example, Tcl waits for either an alarm-clock timeout or a result from the query. The same method can be used to wait for file or socket events, or to respond to user actions in a Tk interface, while also waiting for query results. Note: this is not a complete script.

Example5.19.Asynchronous Query with Event-Driven Results

    # 時間切れ把持子｡時間切れ TIMEOUT を示す大域旗印を設定する.
    # Timeout handler. It sets a global flag to indicate TIMEOUT.
    proc event_timeout {} {
      global event_flag
      if {$event_flag eq "WAITING"} {
        set event_flag TIMEOUT
      }
    }

    # 問合せ準備把持子｡完了READYを示す大域旗印を設定する｡.
    # Query ready handler. It sets a global flag to indicate READY.
    proc event_result {} {
      global event_flag
      if {$event_flag eq "WAITING"} {
        set event_flag READY
      }
    }

    # 時間切れを伴う問合せ発信｡
    #   conn はデータベース接続把持子｡
    #   sql は 実行すべき SQL 文書｡.
    #   timeout is the number of seconds to wait for the query.
    #   時間切れは､問合せを待つべき秒の数｡.
    # もし､問合せが完了するとこの譜は行数を表示する｡実使用では､,
    # 結果を基に何かを更にできるであろう｡.
    # Issue a query with timeout.
    #   conn is the database connection handle.
    #   sql is the SQL text to execute.
    #   timeout is the number of seconds to wait for the query.
    # If the query completes, this just displays the row count. In real life,
    # you would do something more with the results.
    proc query_timeout {conn sql {timeout 20}} {
      global event_flag

      # 千分の一秒単位で時間切れ計算:
      # Calculate timeout in milliseconds:
      set timeout_msec [expr {1000 * $timeout}]

      # 大域旗印の初期化:
      # Initialize the global flag:
      set event_flag WAITING

      # 結果呼戻しの確立:
      # Establish the result callback:
      pg_result_callback $conn event_result

      # 時間切れの設定:
      # Set the alarm clock:
      set after_id [after $timeout_msec event_timeout]

      # 問合せ断を送るが､待たない:
      # Send the query off, but do not wait:
      pg_sendquery $conn $sql

      # 何かを待つ - 時間切れや結果 -大域旗印を変更する為に:
      # Wait for something - timeout or results - to change the global flag:
      vwait event_flag

      # 問合せは完了したか?
      # Did the query complete?
      if {$event_flag eq "READY"} {
        # 警告時刻を取消す:
        # Cancel the alarm clock:
        after cancel $after_id
        puts "Query complete. Fetching result:"
        set res [pg_getresult $conn]
        puts "The query returned [pg_result $res -numTuples] row(s)."
        pg_result $res -clear
      } elseif {$event_flag eq "TIMEOUT"} {
        puts "Query did not complete! It timed out in about $timeout seconds."
        puts "Canceling the request:"
        # 註意 pg_cancelrequest も又､結果呼戻しを解消する｡
        # Note pg_cancelrequest also cancels the result callback.
        pg_cancelrequest $conn
        # 今や､背景処理が準備できる迄阻止せねばならぬ｡
        # Now we have to block until the backend is ready.
        pg_result [pg_getresult $conn] -clear
        puts "Request canceled."
      }
    }

これは､上記函数を使った用例である｡pg_sleep PostgreSQL 背景函数(PostgreSQL-8.2.0で追加された)は､遅い問合せの模擬に使われている｡ 問合せ時間が､していされた時間切れより長いので､時間切れになる｡ もし､数字が切替っていると､問合せは完了する｡ Here is an example of using the above function. The pg_sleep PostgreSQL backend function (added at PostgreSQL-8.2.0) is used to simulate a slow query. Because the query time is longer than the specified timeout, this will time out. If the numbers are switched, the query will complete.

      query_timeout $conn "select pg_sleep(6)" 4

Prev | Up | Next
5.11.Example - Asynchronous Queries | Home | 5.13.Example - COPY FROM and COPY TO