Clatter LabsHelp Center

How do I read the event stream in Python?

How-toUpdated
import json, requests

def follow(run_id):
    after = 0
    while True:
        with requests.get(f"{BASE}/v1/runs/{run_id}/events",
                          params={"after": after}, headers=headers, stream=True) as r:
            event = None
            for line in r.iter_lines(decode_unicode=True):
                if line.startswith("event: "):
                    event = line[7:]
                elif line.startswith("data: "):
                    data = json.loads(line[6:])
                    if event == "step":
                        after = data["index"] + 1
                        print(data)
                    elif event == "done":
                        return data
        # timeout or dropped connection: loop and reconnect

This is the client from the Streaming guide.

Related articles

Still need help?

Tried the steps above and still stuck? Email us. We usually reply within the first hour, and always within 24 hours. A full support team is with you.

Infrastructure questions, or permanent deletion of your data
infrastructure@clatterlabs.xyz
For deleting your whole account or the data of one service.