Skip to content

Commit

Permalink
Fix logsparse on multiprocess spawn (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaogaotiantian authored Oct 10, 2024
1 parent 4575ed8 commit 8f65505
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/viztracer/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ def run(self) -> None:
tracer = viztracer.VizTracer(**self._viztracer_kwargs)
install_all_hooks(tracer, self._cmdline_args)
tracer.register_exit()
tracer.start()
if not self._viztracer_kwargs.get("log_sparse"):
tracer.start()
self._run()


Expand Down
29 changes: 29 additions & 0 deletions tests/test_logsparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ def f(x):
time.sleep(0.1)
"""

file_multiprocess_spawn = """
import multiprocessing
from viztracer import get_tracer
def bar():
pass
def child(it):
for idx in it:
with get_tracer().log_event(f"custom_event"):
bar()
def main():
p = multiprocessing.get_context("spawn").Process(target=child, args=(range(3), ))
p.start()
p.join()
if __name__ == "__main__":
main()
"""

file_context_manager = """
from viztracer import VizTracer, log_sparse
Expand Down Expand Up @@ -199,6 +220,14 @@ def test_multiprocess(self):
if not os.getenv("COVERAGE_RUN"):
raise e

def test_multiprocess_spawn(self):
self.template(["viztracer", "-o", "result.json", "--log_sparse", "cmdline_test.py"],
script=file_multiprocess_spawn,
expected_output_file="result.json",
expected_entries=3,
check_func=functools.partial(self.check_func, target=['custom_event'] * 3),
concurrency="multiprocessing")

def test_context_manager(self):
self.template(["python", "cmdline_test.py"], script=file_context_manager,
expected_output_file="result.json", expected_entries=4,
Expand Down

0 comments on commit 8f65505

Please sign in to comment.