تکرار کننده های جریانی

متد search_stream یک تکرارکننده از اشیاء SearchGoogleAdsStreamResponse را برمی‌گرداند.

همانطور که در این مثال کد نشان داده شده است، می‌توانید در هر ردیف GoogleAdsRow در فیلد results هر پاسخ، پیمایش کنید.

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 توسط فیلدهایی که در کوئری زبان جستجوی گوگل ادز (GAQL) خود انتخاب می‌کنید، تعیین می‌شود. برای جزئیات بیشتر در مورد ساختار پاسخ، به زبان جستجوی گوگل ادز مراجعه کنید.

هنگام فراخوانی GoogleAdsService.search_stream ، یک تکرارکننده پاسخ جریانی بازگردانده می‌شود. این تکرارکننده باید در حین استفاده در همان محدوده کلاینت GoogleAdsService باقی بماند تا از جریان‌های شکسته یا خطاهای تقسیم‌بندی جلوگیری شود. دلیل این امر این است که شیء Channel gRPC پس از خارج شدن شیء باز GoogleAdsService از محدوده، جمع‌آوری زباله می‌شود. اگر شیء GoogleAdsService تا زمانی که تکرار روی نتیجه search_stream رخ می‌دهد، دیگر در محدوده نباشد، ممکن است شیء Channel از قبل نابود شده باشد و باعث ایجاد رفتار نامشخصی هنگام تلاش تکرارکننده برای بازیابی مقدار بعدی شود.

کد زیر استفاده نادرست از تکرارکننده‌های جریانی را نشان می‌دهد:

def stream_response(client, customer_id, query):
    return client.get_service("GoogleAdsService", version="v24").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="v24")
    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.