Skip to content

Commit

Permalink
Force profile output when triggered by --off. (#641)
Browse files Browse the repository at this point in the history
* Force profile output when triggered by --off.

* Fixed for Windows.
  • Loading branch information
emeryberger authored Jul 25, 2023
1 parent 1051631 commit 63af1fe
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions scalene/scalene_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,9 +951,15 @@ def output_profile() -> bool:
# be used by the parent process
if "is_child" in json_output:
return True
if not Scalene.__output.output_file:
Scalene.__output.output_file = "/dev/stdout"
with open(Scalene.__output.output_file, "w") as f:
outfile = Scalene.__output.output_file
# If there was no output file specified, print to the console.
if not outfile:
if sys.platform == "win32":
outfile = "CON"
else:
outfile = "/dev/stdout"
# Write the JSON to the output file (or console).
with open(outfile, "w") as f:
f.write(
json.dumps(json_output, sort_keys=True, indent=4) + "\n"
)
Expand Down Expand Up @@ -1760,6 +1766,9 @@ def stop_signal_handler(
for pid in Scalene.child_pids:
Scalene.__orig_kill(pid, Scalene.__signals.stop_profiling_signal)
Scalene.stop()
# Output the profile if `--outfile` was set to a file.
if Scalene.__output.output_file:
Scalene.output_profile()

@staticmethod
def disable_signals(retry: bool = True) -> None:
Expand Down

0 comments on commit 63af1fe

Please sign in to comment.