เมื่อเรียกใช้ GoogleAdsService.search_stream
ระบบจะแสดงผลตัวดำเนินการตอบกลับแบบสตรีม ตัวดำเนินการนี้จะอยู่ในขอบเขตเดียวกับไคลเอ็นต์ GoogleAdsService
ขณะใช้งานเพื่อหลีกเลี่ยงสตรีมที่ขาดตอนหรือข้อผิดพลาดในการแบ่งกลุ่ม เนื่องจากระบบจะรวบรวมขยะออบเจ็กต์ gRPC Channel
เมื่อออบเจ็กต์ GoogleAdsService
ที่เปิดอยู่อยู่นอกขอบเขต
หากออบเจ็กต์ GoogleAdsService
ไม่ได้อยู่ในขอบเขตอีกต่อไปเมื่อเกิดการทำซ้ำในผลลัพธ์ของ search_stream
ออบเจ็กต์ Channel
อาจถูกทำลายไปแล้ว ซึ่งจะทำให้เกิดลักษณะการทำงานที่ไม่ระบุเมื่อตัวระบุเส้นทางพยายามดึงค่าถัดไป
โค้ดต่อไปนี้แสดงการใช้ตัวจัดเรียงสตรีมที่ไม่ถูกต้อง
def stream_response(client, customer_id, query):
return client.get_service("GoogleAdsService", version="v18").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="v18")
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.