[Python] Gevent monitoring: printing stacktrace on blocked greenlets

Discussão em 'Python' iniciado por Stack, Maio 16, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to set up the monitoring thread in gevent to detect a blocked Celery worker and to print the stacktrace of the blocked worker. We set up the monitoring thread like this:

    def show_thread_info(hub):
    frames = sys._current_frames()
    for thread_id, frame in frames.items():
    # our company's logger
    log.info(
    extra={"stack": traceback.extract_stack(frame)},
    )


    def setup_monitoring():
    hub = get_hub()
    hub.periodic_monitoring_thread.add_monitoring_function(show_thread_info, 3)

    @worker_ready.connect
    def on_worker_ready(**kwargs):
    setup_monitoring()


    Doing it the above way constantly prints a stack trace, how can I set it up such that this stack trace only gets printed when a blocked worker is detected?

    I see in the docs that you can enable GEVENT_MAX_BLOCKING_TIME to detect a block, but it says this depends on the callback set in greenlet.settrace and that this callback should be set up before the monitoring thread is set up. However, setting greenlet.settrace with my stack trace function also causes things to constantly get printed.

    How can I set it up that it prints a stack trace for workers that are blocked?

    Continue reading...

Compartilhe esta Página