Skip to content

Commit

Permalink
Don't compile Python code in get_py_span_context
Browse files Browse the repository at this point in the history
It's a lot faster to just use `getattr`/`call0` to call Python functions
instead of compiling a Python module each time the function is called.
  • Loading branch information
sk1p committed Nov 19, 2023
1 parent 6a1f01e commit 9756996
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
10 changes: 6 additions & 4 deletions libertem_k2is/examples/fast_acq_integrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def cast_and_add(dst, src):
dst_flat[i] += src_flat[i]


def iterate(outer_i, aq, cam, cam_client, frame_arr):
def iterate(outer_i, aq, cam, cam_client, frame_arr, do_work):
t0 = time.time()
i = 0
try:
Expand All @@ -55,7 +55,8 @@ def iterate(outer_i, aq, cam, cam_client, frame_arr):
payload = np.frombuffer(mv, dtype=np.uint16).reshape(
cam.get_frame_shape()
)
cast_and_add(frame_arr, payload)
if do_work:
cast_and_add(frame_arr, payload)
# frame_arr += payload
cam_client.done(slot)
finally:
Expand All @@ -68,7 +69,8 @@ def iterate(outer_i, aq, cam, cam_client, frame_arr):
@click.option('--mode', default="summit", type=str)
@click.option('--num-parts', default=4, type=int)
@click.option('--frames-per-part', default=10, type=int)
def main(mode, num_parts, frames_per_part):
@click.option('--do-work', default=True, type=bool)
def main(mode, num_parts, frames_per_part, do_work):
maybe_setup_tracing("write_and_iterate")
with tracer.start_as_current_span("main"):
mode = Mode.from_string(mode)
Expand Down Expand Up @@ -105,7 +107,7 @@ def main(mode, num_parts, frames_per_part):
aq = cam.make_acquisition(aqp)
print(f"acquisition {aq}")
cam.wait_for_start()
iterate(i, aq, cam, cam_client, frame_arr)
iterate(i, aq, cam, cam_client, frame_arr, do_work)
finally:
# this shuts down the runtime, backgrounds threads and all...
cam.stop()
Expand Down
16 changes: 5 additions & 11 deletions libertem_k2is/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,11 @@ fn tracing_thread() {
}

fn get_py_span_context(py: Python) -> PyResult<SpanContext> {
let extract_span_context = PyModule::from_code(
py,
"
from opentelemetry import trace
span = trace.get_current_span()
span_context = span.get_span_context()",
"",
"",
)?;

let span_context_py = extract_span_context.getattr("span_context")?;
let span_context_py = PyModule::import(py, "opentelemetry.trace")?
.getattr("get_current_span")?
.call0()?
.getattr("get_span_context")?
.call0()?;

let trace_id_py: u128 = span_context_py.getattr("trace_id")?.extract()?;
let span_id_py: u64 = span_context_py.getattr("span_id")?.extract()?;
Expand Down

0 comments on commit 9756996

Please sign in to comment.