উত্তর স্ট্রিম ইটারেটর

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 Ads Query Language (GAQL) কোয়েরিতে বেছে নেওয়া ফিল্ডের মাধ্যমে নির্ধারিত হয়। উত্তর স্ট্রাকচার সম্পর্কে আরও বিবরণের জন্য, Google Ads কোয়েরি ল্যাঙ্গুয়েজ দেখুন।

ইটারেটরকে স্কোপে রাখুন

কল করার সময় GoogleAdsService.search_stream, একটি স্ট্রিমিং রেসপন্স ইটারেটর রিটার্ন করা হয়। এই ইটারেটরকে অবশ্যই GoogleAdsService ক্লায়েন্টের মতো একই স্কোপে থাকতে হবে, যাতে ব্রোকেন স্ট্রিম বা সেগমেন্টেশন ফল্ট এড়ানো যায়। এর কারণ হল, ওপেন GoogleAdsService অবজেক্ট স্কোপের বাইরে চলে গেলে, অন্তর্নিহিত gRPC Channel অবজেক্টটি গার্বেজ-কালেক্ট করা হয়। search_stream-এর ফলাফলে ইটারেশন হওয়ার সময় GoogleAdsService অবজেক্ট আর স্কোপে না থাকলে, Channel অবজেক্ট হয়ত আগেই নষ্ট হয়ে গেছে, যার ফলে ইটারেটর পরবর্তী ভ্যালু উদ্ধার করার চেষ্টা করলে, অনির্দিষ্ট আচরণ দেখা যায়।

নিচের কোডটি স্ট্রিম ইটারেটরের ভুল ব্যবহার দেখায়:

def stream_response(client, customer_id, query):
    # The GoogleAdsService instance goes out of scope when this function
    # returns.
    ga_service = client.get_service(
        "GoogleAdsService", version="v26"
    )
    return ga_service.search_stream(customer_id=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)

    # Accessing the iterator in a different scope from where the service object
    # was created results in undefined behavior.
    for batch in response:
        print(batch)

এই কোডে, GoogleAdsService অবজেক্টটি অন্য স্কোপের মধ্যে তৈরি করা হয়েছে যেখান থেকে ইটারেটর অ্যাক্সেস করা হয়। এর ফলে, ইটারেটর সম্পূর্ণ উত্তর ব্যবহার করার আগেই Channel অবজেক্টটি নষ্ট হয়ে যেতে পারে।

এর পরিবর্তে, GoogleAdsService ক্লায়েন্ট যতক্ষণ ব্যবহার করা হচ্ছে ততক্ষণ স্ট্রিমিং ইটারেটরকে একই স্কোপে রাখুন:

def main(client, customer_id):
    ga_service = client.get_service(
        "GoogleAdsService", version="v26"
    )
    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 where the service object was
    # created.
    for batch in response:
        print(batch)