Skip to content

Commit

Permalink
Fix logsparse on multiprocess spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
gaogaotiantian committed Oct 10, 2024
1 parent 4575ed8 commit 7d29662
Show file tree
Hide file tree
Showing 2 changed files with 33 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
31 changes: 31 additions & 0 deletions tests/test_logsparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,29 @@ def f(x):
time.sleep(0.1)
"""

file_multiprocess_spawn = """
import multiprocessing
import multiprocessing.pool
from viztracer import get_tracer
def bar():
pass
def child(idx):
with get_tracer().log_event("custom_event"):
bar()
def main():
with multiprocessing.pool.Pool(
2,
context=multiprocessing.get_context('spawn'),
) as pool:
pool.map(child, range(3))
if __name__ == "__main__":
main()
"""

file_context_manager = """
from viztracer import VizTracer, log_sparse
Expand Down Expand Up @@ -199,6 +222,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 7d29662

Please sign in to comment.