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

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

次のコードは、ストリーミング イテレータの不適切な使用を示しています。

def stream_response(client, customer_id, query):
    return client.get_service("GoogleAdsService", version="v19").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="v19")
    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.