[Python] How to detect pdb breakpoint in a timer func

Discussão em 'Python' iniciado por Stack, Setembro 27, 2024.

  1. Stack

    Stack Membro Participativo

    I have a timeout func to kill another func that is provided by other users. I want to skip termination if the code running in debugger.

    If they run the whole script in debugger, I can notice this by

    hasattr(sys, 'gettrace') and sys.gettrace() is not None


    However, this doesnt work with pdb breakpoint() in their code since my timer func is in another thread.

    How can I notice if we are in breakpoint()?

    def timeout_func(th:threading.Thread):
    if hasattr(sys, 'gettrace') and sys.gettrace() is not None:
    print("Happy debugging :)")
    return
    logger.critical(f"Thread {th} timed-out, raise exception in it.")
    a_func_to_kill_thread(th) # sends a exception to thread


    # gettrace is set if the whole script is in debugger
    # but it is not yet set for breakpoint in user code

    timeout_timer = threading.Timer(
    interval=timeout,
    function=timeout_func,
    args=[thread_to_be_terminated])
    timeout_timer.name = "Timeout timer"
    timeout_timer.start()

    # run user code now
    # it might be running in the main or its own thread, thread_to_be_terminated




    I am using Python3

    I tried passing sys module to my timeout_func but didnt work. I could find a way to query thread object to get such info.

    I can try to set an environment variable by overriding settrace (which is also used by breakpoint(). However, it is not a good approach.

    Continue reading...

Compartilhe esta Página