Skip to content

Conversation

@MustafaJafar
Copy link
Member

Changelog Description

  • Fix do_profile decorator

Testing notes:

In AYON console, running these cases should work:

import os
from ayon_core.lib.profiling import do_profile

@do_profile
def get_bundle_settings(bundle_name):
    import ayon_api 

    return ayon_api.get_addons_settings(bundle_name)


bundle_name = os.getenv("AYON_BUNDLE_NAME")
settings = get_bundle_settings(bundle_name)
import os
from ayon_core.lib.profiling import do_profile

@do_profile()
def get_bundle_settings(bundle_name):
    import ayon_api 

    return ayon_api.get_addons_settings(bundle_name)


bundle_name = os.getenv("AYON_BUNDLE_NAME")
settings = get_bundle_settings(bundle_name)
import os
from ayon_core.lib.profiling import do_profile

@do_profile(r"c:\dev\data.prof")
def get_bundle_settings(bundle_name):
    import ayon_api 

    return ayon_api.get_addons_settings(bundle_name)


bundle_name = os.getenv("AYON_BUNDLE_NAME")
settings = get_bundle_settings(bundle_name)

@MustafaJafar MustafaJafar self-assigned this Jan 27, 2026
@MustafaJafar MustafaJafar added the type: bug Something isn't working label Jan 27, 2026
@ynbot ynbot added the size/XS label Jan 27, 2026
@MustafaJafar MustafaJafar linked an issue Jan 27, 2026 that may be closed by this pull request
2 tasks
Comment on lines +8 to +42
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
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

@ynbot ynbot moved this to Review In Progress in PR reviewing Jan 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XS type: bug Something isn't working

Projects

Status: Review In Progress

Development

Successfully merging this pull request may close these issues.

do_profile doesn't work as a decorator

4 participants