-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: jupyter support, using cell magic
- Loading branch information
1 parent
89d953e
commit d6796ce
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import os | ||
import tempfile | ||
import viztracer | ||
from viztracer.report_builder import ReportBuilder | ||
from IPython.display import HTML, display | ||
|
||
from IPython.core.magic import (cell_magic, | ||
magics_class, | ||
Magics, | ||
needs_local_scope, | ||
) | ||
|
||
|
||
from .giltracer import GilTracer | ||
|
||
@magics_class | ||
class GilTraceMagic(Magics): | ||
@needs_local_scope | ||
@cell_magic | ||
def giltrace(self, line, cell, local_ns): | ||
temp_dir = tempfile.mkdtemp() | ||
perf_path = os.path.join(temp_dir, 'perf.data') | ||
viz_path = os.path.join(temp_dir, 'viztracer.json') | ||
gil_path = os.path.join(temp_dir, 'giltracer.json') | ||
out_path = 'result.html' | ||
code = self.shell.transform_cell(cell) | ||
with GilTracer(perf_path, gil_path) as gt: | ||
with viztracer.VizTracer(output_file=viz_path): | ||
exec(code, local_ns, local_ns) | ||
builder = ReportBuilder([viz_path, gil_path]) | ||
builder.save(output_file=out_path) | ||
|
||
download = HTML(f'''<a href="{out_path}" download>Download {out_path}</a>''') | ||
view = HTML(f'''<a href="{out_path}" target="_blank" rel=”noopener noreferrer”>Open {out_path} in new tab</a> (might not work due to security issue)''') | ||
display(download, view) | ||
|
||
def load_ipython_extension(ipython): | ||
""" | ||
Use `%load_ext per4m.cellmagic` | ||
""" | ||
ipython.register_magics(GilTraceMagic) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# same as example1, but without explicit viztracer calls | ||
import threading | ||
import time | ||
import time | ||
|
||
|
||
def run(): | ||
total = 0 | ||
for i in range(1_000_000): | ||
total += i | ||
return total | ||
|
||
|
||
thread1 = threading.Thread(target=run) | ||
thread2 = threading.Thread(target=run) | ||
thread1.start() | ||
thread2.start() | ||
time.sleep(0.2) | ||
for thread in [thread1, thread2]: | ||
thread.join() |