Repository navigation
Description
The context
I was trying to restart my pet project of extending profiling into child Python processes (e.g. via subprocess, multiprocessing, and os.fork()) with kernprof (or whatever its successor in 6.0 will be called), which in some previous PRs I've been gradually working towards. It may need a bit elbow grease and boilerplate to port all the stuff over, but it should be very doable: after all, I've already written a pytest plugin which does that for tests, and kernprof is kinda like pytest in that both run other Python code in a somewhat controlled environment.
The test
To gauge how much work is ahead of us and to demonstrate what exactly the change will be good for, I needed a test use-case. Say we have a script which goes
`multiproc-test.py`
from multiprocessing import Pool
def my_sum(x: list[int]) -> int:
result: int = 0
for item in x:
result += item
return result
def main(length: int = 1000, n: int = 4) -> None:
my_list: list[int] = list(range(length))
sublists: list[list[int]] = []
subsums: list[int]
sublength = length // n
if sublength * n < length:
sublength += 1
while my_list:
sublist, my_list = my_list[:sublength], my_list[sublength:]
sublists.append(sublist)
with Pool(n) as pool:
subsums = pool.map(my_sum, sublists)
pool.close()
pool.join()
print(my_sum(subsums))
if __name__ == '__main__':
main() # stdout: '499500\n'... so just a very simple scenario where we distribute the workload of summing integers over multiple processes. What I expected to happen was that kernprof would only see the one call to my_sum() we made in the main process and report data thereon, but unfortunately...
The bug
... the code didn't even run.
$ kernprof -q multiproc-test.py
AttributeError: module '__main__' has no attribute 'my_sum'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
...
_pickle.PicklingError: Can't pickle <function my_sum at 0x105db62a0>: it's not found as __main__.my_sum
when serializing tuple item 0
when serializing tuple item 0
when serializing tuple item 3Note how we didn't even set up (line-)profiling and yet the execution still failed. The crux of the issue is that for most of the modes under which kernprof runs stuff (except for kernprof --prof-mod=<some_explicit_target> --line-by-line -m <some_module>), the namespace in which the code is executed is not vars(sys.modules['__main__']), which results in errors when multiprocessing.Pool.map() looks in the latter to find the function to pickle and send to the child processes.
An easy fix for the bug is that as with line_profiler.autoprofile.autoprofile.run(..., as_module=True), we always use a dummy module object and its namespace, and monkey-patch it into sys.modules['__main__'], regardless of whether the code is run as a module.
Notes
This issue is mostly just for contextualization and documentation; just need to also whip up some tests and I'll push a PR to fix it.
Activity
[-]`kernprof` executed code inconsistent with `sys.modules['__main__']`, causing pickling issues[/-][+]BUG: `kernprof` executed code inconsistent with `sys.modules['__main__']`, causing pickling issues[/+]Added tests against issue pyutils#422
Erotemic commentedon Mar 7, 2026
So this is only an issue because you have your multiprocessing code in
__main__, if you had it in a separate module, then it at least would not cause pickle issues? And we are effectively doing this monkey patching anyway in autoprofile, so it's not a new thing to do it in kernprof itself?TTsangSC commentedon Mar 7, 2026
Yes to the latter part (we already do that for autoprof in some cases, as you've noted); probably yes for the former, seeing that this is just a namespace issue. I see that you've already left a comment over at the PR too, will check in a moment.
TTsangSC commentedon Mar 7, 2026
I have both good and bad news. TLDR: what you've noted is entirely right (so #423 should be clear to go), but I discovered another bug involving duplicate functions not "seen" by the profiler.
Good news
Yes, indeed the problem is just caused by
_pickle's not finding the requisite object at__main__. So if one were to e.g. move themy_sum()to a module and import it in the script (which contains themain()andmultiprocessingand what's not), everything would be fine even onHEAD, because the function passed toPool.map()is not from the problematic namespace.Bad news
To test this I just moved the original
multiproc-test.pyto somewhere where from which it can be imported (say tosome-dir-on-my-python-path/mp_test_module.py), and made a copy (saymp-test-script.py) as the script to be run, where the only difference is that it importsmy_sum()from the former instead of defining it locally.`mp-test-script.py`
So, running
kernprof -l -prof-mod=mp_test_module mp-test-script.pydoes "work" as expected – the script executes fine, and the profiler catches the one call tomy_sum()we made directly inmain(), but not the other 4 calls made in child processes.HOWEVER, this resulted in some rather unexpected profiler output:
(terminal output, pathological case)
Note how
mp_test_module.main()is somehow getting hits which are all on the wrong lines. What happened was probably that:mp_test_module.main()andmp-test-script.py::main()has the same source code, so they compile down to the same bytecode.mp_test_modulebut notmp-test-script.py, it only sees themain()in the former.main()is seen by the profiler, it never knows about the dupe and doesn't do the usual padding and de-duplication.main()s are unmodified, whenmp-test-script.py::main()is executed, some of its line hashes would overlap with those ofmp_test_module.main(), which the profiler then incorrectly surmises has been called.Indeed, if one were to profile both
mp_test_moduleandmp-test-script.py, the profiling data is correctly attributed, because the profiler has seen bothmain()s and swapped out the bytecode on one of them:(click to expand)
Seems that even after the discussions and fixes we've had over #348–#351 there are still edge cases unaccounted for. Possible fixes are that either:
code.co_name,.co_filename, and.co_firstlineno); orOut of the two, the second is probably way faster since we don't increase the per-line-event overhead.
Will write a separate issue and PR to fix that.
FIX: `kernprof` to always run code in `sys.modules['__main__']`'s nam…
TTsangSC commentedon Mar 7, 2026
Oops I forgot to auto-close.