স্ট্রিমিং পুনরাবৃত্তিকারী
সেভ করা পৃষ্ঠা গুছিয়ে রাখতে 'সংগ্রহ' ব্যবহার করুন
আপনার পছন্দ অনুযায়ী কন্টেন্ট সেভ করুন ও সঠিক বিভাগে রাখুন।
GoogleAdsService.search_stream
কল করার সময়, একটি স্ট্রিমিং প্রতিক্রিয়া পুনরাবৃত্তিকারী ফিরে আসে। ভাঙা স্ট্রিম বা বিভাজন ত্রুটিগুলি এড়াতে ব্যবহার করার সময় এই পুনরাবৃত্তিকারীটি GoogleAdsService
ক্লায়েন্টের মতো একই সুযোগে থাকা উচিত। এর কারণ হল GRPC Channel
অবজেক্টটি আবর্জনা-সংগ্রহ করা হয় একবার খোলা GoogleAdsService
অবজেক্টটি সুযোগের বাইরে চলে যায়। যদি GoogleAdsService
অবজেক্টটি search_stream
এর ফলাফলে পুনরাবৃত্তি ঘটানোর সময় আর সুযোগে না থাকে, তাহলে Channel
অবজেক্টটি ইতিমধ্যেই ধ্বংস হয়ে যেতে পারে, যখন পুনরাবৃত্তিকারী পরবর্তী মানটি পুনরুদ্ধার করার চেষ্টা করে তখন অনির্ধারিত আচরণের সৃষ্টি করে।
নিম্নলিখিত কোড স্ট্রিমিং পুনরাবৃত্তির ভুল ব্যবহার প্রদর্শন করে:
def stream_response(client, customer_id, query):
return client.get_service("GoogleAdsService", version="v21").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="v21")
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.
অন্য কিছু উল্লেখ না করা থাকলে, এই পৃষ্ঠার কন্টেন্ট Creative Commons Attribution 4.0 License-এর অধীনে এবং কোডের নমুনাগুলি Apache 2.0 License-এর অধীনে লাইসেন্স প্রাপ্ত। আরও জানতে, Google Developers সাইট নীতি দেখুন। Java হল Oracle এবং/অথবা তার অ্যাফিলিয়েট সংস্থার রেজিস্টার্ড ট্রেডমার্ক।
2025-08-26 UTC-তে শেষবার আপডেট করা হয়েছে।
[null,null,["2025-08-26 UTC-তে শেষবার আপডেট করা হয়েছে।"],[[["\u003cp\u003eWhen using \u003ccode\u003eGoogleAdsService.search_stream\u003c/code\u003e, the streaming response iterator must remain in the same scope as the \u003ccode\u003eGoogleAdsService\u003c/code\u003e client to prevent stream disruptions or segmentation faults.\u003c/p\u003e\n"],["\u003cp\u003eThis is because the underlying gRPC \u003ccode\u003eChannel\u003c/code\u003e object can be garbage-collected if the \u003ccode\u003eGoogleAdsService\u003c/code\u003e object goes out of scope before the iterator is fully consumed.\u003c/p\u003e\n"],["\u003cp\u003eAccessing the iterator in a different scope may lead to undefined behavior as the \u003ccode\u003eChannel\u003c/code\u003e might be destroyed prematurely.\u003c/p\u003e\n"],["\u003cp\u003eEnsure the iterator is used within the same scope where the \u003ccode\u003eGoogleAdsService\u003c/code\u003e object is created to guarantee successful iteration through the response.\u003c/p\u003e\n"]]],[],null,["# Streaming Iterators\n\nWhen calling\n[`GoogleAdsService.search_stream`](/google-ads/api/reference/rpc/v21/GoogleAdsService/SearchStream),\na streaming response iterator is returned. This iterator should remain in the\nsame scope as the `GoogleAdsService` client while being used in order to avoid\nbroken streams or segmentation faults. This is because the gRPC `Channel` object\nis garbage-collected once the open `GoogleAdsService` object goes out of scope.\nIf the `GoogleAdsService` object is no longer in scope by the time the iteration\noccurs on the result of `search_stream`, the `Channel` object may already be\ndestroyed, causing undefined behavior when the iterator attempts to retrieve the\nnext value.\n\nThe following code demonstrates *incorrect* usage of streaming iterators: \n\n def stream_response(client, customer_id, query):\n return client.get_service(\"GoogleAdsService\", version=\"v21\").search_stream(customer_id, query=query)\n\n def main(client, customer_id):\n query = \"SELECT campaign.name FROM campaign LIMIT 10\"\n response = stream_response(client, customer_id, query=query)\n # Access the iterator in a different scope from where the service object was created.\n try:\n for batch in response:\n # Iterate through response, expect undefined behavior.\n\nIn the above code, the `GoogleAdsService` object is created within a different\nscope from where the iterator is accessed. As a result, the `Channel` object may\nbe destroyed before the iterator consumes the entire response.\n\nInstead, the streaming iterator should remain in the same scope as the\n`GoogleAdsService` client for as long as it is being used: \n\n def main(client, customer_id):\n ga_service = client.get_service(\"GoogleAdsService\", version=\"v21\")\n query = \"SELECT campaign.name FROM campaign LIMIT 10\"\n response = ga_service.search_stream(customer_id=customer_id, query=query)\n # Access the iterator in the same scope as where the service object was created.\n try:\n for batch in response:\n # Successfully iterate through response."]]