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)