From 219eaa2ff9702f6ee7d628721b0a857d331278ea Mon Sep 17 00:00:00 2001 From: MustafaJafar Date: Sat, 10 Jan 2026 03:41:05 +0200 Subject: [PATCH 1/3] Update `do_profile` decorator --- client/ayon_core/lib/profiling.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/client/ayon_core/lib/profiling.py b/client/ayon_core/lib/profiling.py index f3a31462006..3b5953a5287 100644 --- a/client/ayon_core/lib/profiling.py +++ b/client/ayon_core/lib/profiling.py @@ -4,7 +4,7 @@ import cProfile -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: @@ -15,15 +15,18 @@ def do_profile(fn, to_file=None): 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() + def _do_profile(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 + return _do_profile From 3881aacb4427f9d3bdce7aaa418713b66c3bcb98 Mon Sep 17 00:00:00 2001 From: MustafaJafar Date: Sat, 10 Jan 2026 03:57:00 +0200 Subject: [PATCH 2/3] `do_profile`: add `functools.wraps` --- client/ayon_core/lib/profiling.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/client/ayon_core/lib/profiling.py b/client/ayon_core/lib/profiling.py index 3b5953a5287..fbbdf5b7ded 100644 --- a/client/ayon_core/lib/profiling.py +++ b/client/ayon_core/lib/profiling.py @@ -2,6 +2,7 @@ """Provide profiling decorator.""" import os import cProfile +import functools def do_profile(to_file=None): @@ -16,6 +17,7 @@ def do_profile(to_file=None): to_file = to_file.format(pid=os.getpid()) def _do_profile(fn): + @functools.wraps(fn) def profiled(*args, **kwargs): profiler = cProfile.Profile() try: From d542df93d28e8b5a204237642bb09bb3aab2ab90 Mon Sep 17 00:00:00 2001 From: MustafaJafar Date: Tue, 27 Jan 2026 14:23:08 +0200 Subject: [PATCH 3/3] `do_profile`: support calling the decorator without parentheses `()` --- client/ayon_core/lib/profiling.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/client/ayon_core/lib/profiling.py b/client/ayon_core/lib/profiling.py index fbbdf5b7ded..e6d8714b3db 100644 --- a/client/ayon_core/lib/profiling.py +++ b/client/ayon_core/lib/profiling.py @@ -13,8 +13,6 @@ def do_profile(to_file=None): instead of printing. """ - if to_file: - to_file = to_file.format(pid=os.getpid()) def _do_profile(fn): @functools.wraps(fn) @@ -31,4 +29,14 @@ def profiled(*args, **kwargs): 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()) + return _do_profile