GoogleAdsService.search_stream
を呼び出すと、ストリーミング レスポンスのイテレータが返されます。このイテレータは、ストリームの破損やセグメンテーション違反を防ぐために、使用されている間は GoogleAdsService
クライアントと同じスコープに留める必要があります。これは、開いている GoogleAdsService
オブジェクトがスコープ外になると、gRPC Channel
オブジェクトがガベージ コレクションの対象になるためです。search_stream
の結果でイテレーションが発生するまでに GoogleAdsService
オブジェクトがスコープ外にある場合、Channel
オブジェクトがすでに破棄され、イテレータが次の値を取得しようとしたときに未定義の動作が発生する可能性があります。
次のコードは、ストリーミング イテレータの誤った使用方法を示しています。
def stream_response(client, customer_id, query):
return client.get_service("GoogleAdsService", version="v16").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="v16")
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.