ตัวซ้ำการสตรีม
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
เมื่อโทรหา
GoogleAdsService.search_stream
ระบบจะแสดงผลตัววนซ้ำการตอบกลับแบบสตรีม ตัววนซ้ำนี้ควรอยู่ในขอบเขตเดียวกันกับไคลเอ็นต์ GoogleAdsService
ขณะใช้งานเพื่อหลีกเลี่ยงสตรีมที่ขาดตอนหรือข้อผิดพลาดในการแบ่งส่วน เนื่องจากระบบจะรวบรวมออบเจ็กต์ Channel
ของ gRPC
เมื่อออบเจ็กต์ 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.
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-27 UTC
[null,null,["อัปเดตล่าสุด 2025-08-27 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."]]