Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions client/ayon_core/lib/profiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,41 @@
"""Provide profiling decorator."""
import os
import cProfile
import functools


def do_profile(fn, to_file=None):
def do_profile(to_file=None):
"""Wraps function in profiler run and print stat after it is done.

Args:
to_file (str, optional): If specified, dumps stats into the file
instead of printing.

"""

def _do_profile(fn):
@functools.wraps(fn)
def profiled(*args, **kwargs):
profiler = cProfile.Profile()
try:
profiler.enable()
res = fn(*args, **kwargs)
profiler.disable()
return res
finally:
if to_file:
profiler.dump_stats(to_file)
else:
profiler.print_stats()
return profiled

# If used as @do_profile, to_file is the function
if callable(to_file):
fn = to_file
to_file = None
return _do_profile(fn)

if to_file:
to_file = to_file.format(pid=os.getpid())

def profiled(*args, **kwargs):
profiler = cProfile.Profile()
try:
profiler.enable()
res = fn(*args, **kwargs)
profiler.disable()
return res
finally:
if to_file:
profiler.dump_stats(to_file)
else:
profiler.print_stats()
return _do_profile
Comment on lines +8 to +42
Copy link
Member

@iLLiCiTiT iLLiCiTiT Jan 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually this is done using required kwargs, or it is one of the options.

Suggested change
def do_profile(to_file=None):
"""Wraps function in profiler run and print stat after it is done.
Args:
to_file (str, optional): If specified, dumps stats into the file
instead of printing.
"""
def _do_profile(fn):
@functools.wraps(fn)
def profiled(*args, **kwargs):
profiler = cProfile.Profile()
try:
profiler.enable()
res = fn(*args, **kwargs)
profiler.disable()
return res
finally:
if to_file:
profiler.dump_stats(to_file)
else:
profiler.print_stats()
return profiled
# If used as @do_profile, to_file is the function
if callable(to_file):
fn = to_file
to_file = None
return _do_profile(fn)
if to_file:
to_file = to_file.format(pid=os.getpid())
def profiled(*args, **kwargs):
profiler = cProfile.Profile()
try:
profiler.enable()
res = fn(*args, **kwargs)
profiler.disable()
return res
finally:
if to_file:
profiler.dump_stats(to_file)
else:
profiler.print_stats()
return _do_profile
def do_profile(*args, to_file=None):
"""Wraps function in profiler run and print stat after it is done.
Args:
to_file (str, optional): If specified, dumps stats into the file
instead of printing.
"""
def _do_profile(fn):
@functools.wraps(fn)
def profiled(*args, **kwargs):
profiler = cProfile.Profile()
try:
profiler.enable()
res = fn(*args, **kwargs)
profiler.disable()
return res
finally:
if to_file:
profiler.dump_stats(to_file)
else:
profiler.print_stats()
return profiled
if to_file:
to_file = to_file.format(pid=os.getpid())
if args:
return _do_profile(args[0])
return _do_profile

Loading