URL Downloader in YTsaurus Flow (Python)

This example shows a pipeline that groups incoming URLs by host and processes them in batches using timers. It demonstrates the typical pattern: “accumulate in state → process by timer → clear state.”

Source code

Structure

The pipeline includes a single transform-computation called url_downloader, which:

  • Accepts messages with host and url fields from the input stream.
  • Accumulates URLs for each host in an internal YSON state.
  • Sets a timer for 5 seconds ahead each time a new URL arrives.
  • When the timer fires, it processes all accumulated URLs and publishes the results to the output stream processed_urls.

url_download_function.py

This file contains the core processing logic. The function implements both RowFunction methods: on_message to accumulate URLs in the state and on_timer to process them in batches.

class UrlDownloadFunction(RowFunction):
    """Groups URLs by host, processes them with a timer-driven loop."""

    def on_message(self, message, output, ctx):
        host = message.payload["host"]
        url = message.payload["url"]

        state = ctx.state("host-state", message)
        data = state.get_or_default({"host": host, "pending_urls": []})
        data["pending_urls"].append(url)
        state.set(data)

        output.add_timer(int(time.time()) + 5)

    def on_timer(self, timer, output, ctx):
        state = ctx.state("host-state", timer)
        data = state.get_or_default(None)
        if not data or not data.get("pending_urls"):
            state.clear()
            return

        host = data["host"]
        for url in list(data["pending_urls"]):
            result = _process_url(url)
            builder = ctx.message_builder("processed_urls")
            builder.set("host", host)
            builder.set("url", url)
            builder.set("data", result)
            output.add_message(builder.finish())
            log.debug("Processed url (Host: %s, Url: %s)", host, url)

        state.clear()

__main__.py

This is the entry point: it creates the pipeline and registers the single url_downloader computation.

def main():
    log.info("Starting companion execution")

    pipeline = Pipeline()
    pipeline.add("url_downloader", UrlDownloadFunction())

    log.info("Starting pipeline...")
    pipeline.run()
    log.info("Pipeline completed")

Key patterns

  • Timer-driven batch processing: output.add_timer(int(time.time()) + 5) in on_message and the processing logic in on_timer provide a standard way to batch events by a time window.
  • Internal YSON state via ctx.state("host-state", message): the get_or_default / set / clear pattern helps you accumulate and clear data.
  • State key by host: you group URLs by host using group_by_schema in the spec, so each computation instance processes URLs for a single host in isolation.
  • MessageBuilder: ctx.message_builder("processed_urls") builds output messages with an explicit stream schema.
  • Safety check in on_timer: if not data or not data.get("pending_urls") prevents reprocessing data after the state is cleared.

See also