INNER CODE UNIT · Python
serialize
hyperonym/basaran · basaran/__main__.py:180
def serialize(data):
data = json.dumps(data, ensure_ascii=False, separators=(",", ":"))
return f"data: {data}\n\n"
def stream():
buffers = {}
times = {}
for choice in stream_model(**options):
index = choice["index"]
now = time.time_ns()
if index not in buffers:
buffers[index] = []
if index not in times:
times[index] = now
buffers[index].append(choice)
# Yield data when exceeded the maximum buffering interval.
elapsed = (now - times[index]) // 1_000_000