Skip to content

Profilers do not work in threads #124656

Open

Description

@spacemanspiff2007

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

Activity

  1. added
    type-bug
    on Sep 27, 2024
  2. ZeroIntensity commented on Sep 27, 2024

    @ZeroIntensity
    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.

  3. added
    docs
    and removed
    type-bug
    on Sep 27, 2024
  4. AA-Turner commented on Sep 27, 2024

    @AA-Turner
    Member

    Recategorising as a documentation problem, a PR is welcome.

    A

  5. gaogaotiantian commented on Sep 27, 2024

    @gaogaotiantian
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    docs

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions