ストリーミング イテレータ

search_stream メソッドは、SearchGoogleAdsStreamResponse オブジェクトのイテレータを返します。

このコード例に示すように、各レスポンスの results フィールドの各 GoogleAdsRow を反復処理できます。

def main(client: GoogleAdsClient, customer_id: str) -> None:
    ga_service: GoogleAdsServiceClient = client.get_service("GoogleAdsService")

    query: str = """
        SELECT
          campaign.id,
          campaign.name
        FROM campaign
        ORDER BY campaign.id"""

    # Issues a search request using streaming.
    stream: Iterator[SearchGoogleAdsStreamResponse] = ga_service.search_stream(
        customer_id=customer_id, query=query
    )

    for batch in stream:
        rows: List[GoogleAdsRow] = batch.results
        for row in rows:
            print(
                f"Campaign with ID {row.campaign.id} and name "
                f'"{row.campaign.name}" was found.'
            )
      

GoogleAdsRow の構造は、Google 広告クエリ言語(GAQL)クエリで選択したフィールドによって決まります。レスポンス構造の詳細については、Google 広告クエリ言語をご覧ください。

GoogleAdsService.search_stream を呼び出すと、ストリーミング レスポンス イテレータが返されます。ストリームの破損やセグメンテーション違反を回避するため、このイテレータは、使用中は GoogleAdsService クライアントと同じスコープ内に留まる必要があります。これは、開いている GoogleAdsService オブジェクトがスコープ外になると、gRPC Channel オブジェクトがガベージ コレクションされるためです。search_stream の結果に対して反復処理が行われる時点で GoogleAdsService オブジェクトがスコープ外になっている場合、Channel オブジェクトがすでに破棄されている可能性があり、イテレータが次の値を取得しようとすると、未定義の動作が発生します。

次のコードは、ストリーミング イテレータの誤った使用方法を示しています。

def stream_response(client, customer_id, query):
    return client.get_service("GoogleAdsService", version="v24").search_stream(customer_id, query=query)

def main(client, customer_id):
    query = "SELECT campaign.name FROM campaign LIMIT 10"
    response = stream_response(client, customer_id, query=query)
    # Access the iterator in a different scope from where the service object was created.
    try:
        for batch in response:
            # Iterate through response, expect undefined behavior.

このコードでは、GoogleAdsService オブジェクトはイテレータがアクセスされるスコープとは異なるスコープ内で作成されます。その結果、イテレータがレスポンス全体を消費する前に Channel オブジェクトが破棄される可能性があります。

代わりに、ストリーミング イテレータは、使用されている限り GoogleAdsService クライアントと同じスコープにとどまる必要があります。

def main(client, customer_id):
    ga_service = client.get_service("GoogleAdsService", version="v24")
    query = "SELECT campaign.name FROM campaign LIMIT 10"
    response = ga_service.search_stream(customer_id=customer_id, query=query)
    # Access the iterator in the same scope as where the service object was created.
    try:
        for batch in response:
            # Successfully iterate through response.