From 3169dbd98c37d777dd1054308529a0f8ef440ee5 Mon Sep 17 00:00:00 2001 From: Shroominic Date: Wed, 30 Oct 2024 14:23:49 +0800 Subject: [PATCH] add chunk timing example --- examples/stream_chunk_timing.py | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 examples/stream_chunk_timing.py diff --git a/examples/stream_chunk_timing.py b/examples/stream_chunk_timing.py new file mode 100644 index 0000000..7a08422 --- /dev/null +++ b/examples/stream_chunk_timing.py @@ -0,0 +1,44 @@ +import asyncio +import time + +from codeboxapi import CodeBox, ExecChunk + + +def sync_stream_exec(cb: CodeBox) -> None: + chunks: list[tuple[ExecChunk, float]] = [] + t0 = time.perf_counter() + for chunk in cb.stream_exec( + "import time;\nfor i in range(3): time.sleep(1); print(i)" + ): + chunks.append((chunk, time.perf_counter() - t0)) + + for chunk, t in chunks: + print(f"{t:.5f}: {chunk}") + + +async def async_stream_exec(cb: CodeBox) -> None: + chunks: list[tuple[ExecChunk, float]] = [] + t0 = time.perf_counter() + async for chunk in cb.astream_exec( + "import time;\nfor i in range(3): time.sleep(1); print(i)" + ): + chunks.append((chunk, time.perf_counter() - t0)) + + for chunk, t in chunks: + print(f"{t:.5f}: {chunk}") + + +print("remote") +cb = CodeBox() +sync_stream_exec(cb) +asyncio.run(async_stream_exec(cb)) + +print("local") +local = CodeBox(api_key="local") +sync_stream_exec(local) +asyncio.run(async_stream_exec(local)) + +print("docker") +docker = CodeBox(api_key="docker") +sync_stream_exec(docker) +asyncio.run(async_stream_exec(docker))