Skip to content

Profilers do not work in threads #124656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
spacemanspiff2007 opened this issue Sep 27, 2024 · 3 comments
Open

Profilers do not work in threads #124656

spacemanspiff2007 opened this issue Sep 27, 2024 · 3 comments
Labels
docs Documentation in the Doc dir

Comments

@spacemanspiff2007
Copy link
Contributor

spacemanspiff2007 commented Sep 27, 2024

Bug report

Bug description:

This issue is related to, but not the same as #110770.
Running two profilers in parallel threads raises an unexpected error.
It's not clear at all that the profiler will not work in parallel.
Either this is a bug or there should be a prominent warning in the docs.

import time
from cProfile import Profile
from threading import Thread


def task_sleep() -> None:

    pr = Profile()
    pr.enable()
    time.sleep(1)
    pr.disable()


t1 = Thread(target=task_sleep)
t2 = Thread(target=task_sleep)

t1.start()
t2.start()

t1.join()
t2.join()
Exception in thread Thread-2 (task_sleep):
ValueError: Another profiling tool is already active

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Linked PRs

@spacemanspiff2007 spacemanspiff2007 added the type-bug An unexpected behavior, bug, or error label Sep 27, 2024
@ZeroIntensity
Copy link
Member

Yeah, you can't use multiple profilers at once. This is sort of documented, see sys.monitoring.use_tool_id:

Raises a ValueError if tool_id is in use.

I guess it's not explicitly mentioned in the cProfile documentation, though.

@AA-Turner AA-Turner added docs Documentation in the Doc dir and removed type-bug An unexpected behavior, bug, or error labels Sep 27, 2024
@AA-Turner
Copy link
Member

Recategorising as a documentation problem, a PR is welcome.

A

@gaogaotiantian
Copy link
Member

This was a new behavior for 3.12, but even before 3.12, this code would not "work" - it wouldn't raise an error either. Anyway, I believe we should document it somewhere in cProfile's doc. @AA-Turner is great with the docs so I'll leave the review to him.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

4 participants