Skip to content

Conversation

@yihong0618
Copy link
Contributor

@yihong0618 yihong0618 commented Nov 23, 2025

This patch fix can not cporfile or profile with multiprocessing

add Co-author @YvesDup with credit.

# cat ttt.py
import concurrent.futures


def worker(x):
    return x * 2


if __name__ == "__main__":
    with concurrent.futures.ProcessPoolExecutor() as executor:
        results = list(executor.map(worker, [1, 2, 3]))
        print(f"Results: {results}")
./python.exe -m profile ttt.py 
./python.exe -m cProfile ttt.py 

and this patch is also expert @gaogaotiantian 's fix idea more can check: #116141

Signed-off-by: yihong0618 <zouzou0208@gmail.com>
Co-Authored-By: YvesDup <yduprat@gmail.com>
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
@YvesDup
Copy link
Contributor

YvesDup commented Nov 24, 2025

@yihong0618 thank you for creating this PR. I was waiting for figure out why or how setting the __spec__ field to None fixed this. And for now, I desperately have no answer.

@YvesDup
Copy link
Contributor

YvesDup commented Nov 24, 2025

./python.exe -m profile ttt.py

About the profile module, see #140729 (comment). Only the cProfile module should be fix.

@yihong0618
Copy link
Contributor Author

./python.exe -m profile ttt.py

About the profile module, see #140729 (comment). Only the cProfile module should be fix.

copy that will fix it

# in the module's namespace.
globs = module.__dict__
globs.update({
'__spec__': spec,
Copy link
Contributor

@YvesDup YvesDup Dec 1, 2025

Choose a reason for hiding this comment

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

By removing spec value to the __spec__key, does the comment above still apppropriate ?
Should not we replace it here with a reference to the issue and @gaogaotiantian original fix ?

Copy link
Member

Choose a reason for hiding this comment

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

So my original "source" is https://docs.python.org/3/reference/import.html#module-specs - where it says the __main__ module normally sets __spec__ to None. But it's for my individual project so as long as it works it's fine. Not sure about the very accurate and official answer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By removing spec value to the __spec__key, does the comment above still apppropriate ? Should not we replace it here with a reference to the issue and @gaogaotiantian original fix ?

remove is not right still fail...we the right is set it to None

Copy link
Contributor

@YvesDup YvesDup Dec 3, 2025

Choose a reason for hiding this comment

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

remove is not right still fail...we the right is set it to None

You are right.

How about adding a comment such as:
See gh-140729: set None to __spec__ according to the documentation (https://docs.python.org/3/reference/import.html#module-specs)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the original fixed from gaotian do not add it, I follow it style~

Signed-off-by: yihong0618 <zouzou0208@gmail.com>
@yihong0618
Copy link
Contributor Author

./python.exe -m profile ttt.py

About the profile module, see #140729 (comment). Only the cProfile module should be fix.

fixed

Signed-off-by: yihong0618 <zouzou0208@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants