স্ট্রিমিং পুনরাবৃত্তিকারী

GoogleAdsService.search_stream কল করার সময়, একটি স্ট্রিমিং প্রতিক্রিয়া পুনরাবৃত্তিকারী ফিরে আসে। ভাঙা স্ট্রিম বা বিভাজন ত্রুটিগুলি এড়াতে ব্যবহার করার সময় এই পুনরাবৃত্তিকারীটি GoogleAdsService ক্লায়েন্টের মতো একই সুযোগে থাকা উচিত। এর কারণ হল GRPC Channel অবজেক্টটি আবর্জনা-সংগ্রহ করা হয় একবার খোলা GoogleAdsService অবজেক্টটি সুযোগের বাইরে চলে যায়। যদি GoogleAdsService অবজেক্টটি search_stream এর ফলাফলে পুনরাবৃত্তি ঘটানোর সময় আর সুযোগে না থাকে, তাহলে 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.