>90% of the time is spent on method ‘acquire’ of ‘thread.lock’ objects
>90% of the time is spent on method ‘acquire’ of ‘thread.lock’ objects
>90% of the time is spent on method ‘acquire’ of ‘thread.lock’ objects
The problem is that you imported send_email inside your method definition. I suggest you to use runctx: cProfile.runctx(‘send_email()’, None, locals()) From the official documentation: cProfile.runctx(command, globals, locals, filename=None) This function is similar to run(), with added arguments to supply the globals and locals dictionaries for the command string.
Updated. You can get output of profiler using io.StringIO() and save it into file. Here is an example: import cProfile import pstats import io def my_func(): result = [] for i in range(10000): result.append(i) return result pr = cProfile.Profile() pr.enable() my_result = my_func() pr.disable() s = io.StringIO() ps = pstats.Stats(pr, stream=s).sort_stats(‘tottime’) ps.print_stats() with open(‘test.txt’, ‘w+’) … Read more
It’s also under Tools->Open cProfile Snapshot
The smaller number is the number of ‘primitive’ or non-recursive calls. The larger number is the total number of invocations, including recursive calls. Since deepcopy is implemented recursively, it means that you called deepcopy directly 1724 times, but that it ended up calling itself ~383k times to copy sub-objects.
Run pytest like this: python3 -m cProfile -o profile -m pytest You can even pass in optional arguments: python3 -m cProfile -o profile -m pytest tests/worker/test_tasks.py -s campaigns This will create a binary file called profile in your current directory. This can be analyzed with pstats: import pstats p = pstats.Stats(‘profile’) p.strip_dirs() p.sort_stats(‘cumtime’) p.print_stats(50) This … Read more
As I mentioned in a comment, when you can’t get cProfile to work externally, you can often use it internally instead. It’s not that hard. For example, when I run with -m cProfile in my Python 2.7, I get effectively the same results you did. But when I manually instrument your example program: import fileinput … Read more
EDIT: Sorry, didn’t realise that the profile call was in a class method. run just tries to exec the string you pass it. If self isn’t bound to anything in the scope of the profiler you are using, you can’t use it in run! Use the runctx method to pass in the local and global … Read more
You’re profiling the process startup, which is why you’re only seeing what happens in p.start() as you say—and p.start() returns once the subprocess is kicked off. You need to profile inside the worker method, which will get called in the subprocesses.
With cProfile you can also profile existing programs, without making any separate profiling script. Just run program with profiler python -m cProfile -o profile_data.pyprof script_to_profile.py and open profile data in kcachegrind with pyprof2calltree, whose -k switch automatically opens data in kcachegrind pyprof2calltree -i profile_data.pyprof -k For example profiling whole paster server and webapp would be … Read more