Skip to content

Commit 3169dbd

Browse files
committed
add chunk timing example
1 parent a45f814 commit 3169dbd

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

examples/stream_chunk_timing.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import asyncio
2+
import time
3+
4+
from codeboxapi import CodeBox, ExecChunk
5+
6+
7+
def sync_stream_exec(cb: CodeBox) -> None:
8+
chunks: list[tuple[ExecChunk, float]] = []
9+
t0 = time.perf_counter()
10+
for chunk in cb.stream_exec(
11+
"import time;\nfor i in range(3): time.sleep(1); print(i)"
12+
):
13+
chunks.append((chunk, time.perf_counter() - t0))
14+
15+
for chunk, t in chunks:
16+
print(f"{t:.5f}: {chunk}")
17+
18+
19+
async def async_stream_exec(cb: CodeBox) -> None:
20+
chunks: list[tuple[ExecChunk, float]] = []
21+
t0 = time.perf_counter()
22+
async for chunk in cb.astream_exec(
23+
"import time;\nfor i in range(3): time.sleep(1); print(i)"
24+
):
25+
chunks.append((chunk, time.perf_counter() - t0))
26+
27+
for chunk, t in chunks:
28+
print(f"{t:.5f}: {chunk}")
29+
30+
31+
print("remote")
32+
cb = CodeBox()
33+
sync_stream_exec(cb)
34+
asyncio.run(async_stream_exec(cb))
35+
36+
print("local")
37+
local = CodeBox(api_key="local")
38+
sync_stream_exec(local)
39+
asyncio.run(async_stream_exec(local))
40+
41+
print("docker")
42+
docker = CodeBox(api_key="docker")
43+
sync_stream_exec(docker)
44+
asyncio.run(async_stream_exec(docker))

0 commit comments

Comments
 (0)