कॉल करते समय
GoogleAdsService.search_stream
एक स्ट्रीमिंग रिस्पॉन्स इटरेटर लौटाया जाता है. यह इटरेटर
GoogleAdsService क्लाइंट के स्कोप से मेल खाता है. ऐसा इसलिए किया जाता है, ताकि
स्ट्रीम या सेगमेंटेशन से जुड़ी गड़बड़ियां. इसकी वजह यह है कि gRPC Channel ऑब्जेक्ट
जब ओपन GoogleAdsService ऑब्जेक्ट, स्कोप से बाहर हो जाता है, तब कचरा इकट्ठा किया जाता है.
अगर बार-बार दोहराए जाने के दौरान, GoogleAdsService ऑब्जेक्ट दायरे में नहीं आता है
search_stream के नतीजे पर मिलता है, तो Channel ऑब्जेक्ट पहले से ही हो सकता है
नष्ट किया जाता है, जब इटरेटर
अगली वैल्यू.
नीचे दिया गया कोड, स्ट्रीमिंग इटरेटर के गलत इस्तेमाल को दिखाता है:
def stream_response(client, customer_id, query):
return client.get_service("GoogleAdsService", version="v22").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="v22")
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.