Skip to content
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

gevent: add atexit close for the hogging detection thread #20

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions easypy/gevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def is_module_patched(*_, **__):
from logging import getLogger
import threading # this will be reloaded after patching

import atexit
import time
import sys

Expand All @@ -26,6 +27,8 @@ def is_module_patched(*_, **__):

HOGGING_TIMEOUT = 1

_HOGGING_DETECTION_RUNNING = False


def apply_patch(hogging_detection=False, real_threads=1):
_logger.info('applying gevent patch (%s real threads)', real_threads)
Expand Down Expand Up @@ -63,7 +66,15 @@ def apply_patch(hogging_detection=False, real_threads=1):
if hogging_detection:
import greenlet
greenlet.settrace(lambda *args: _greenlet_trace_func(*args))
defer_to_thread(detect_hogging, 'detect-hogging')
global _HOGGING_DETECTION_RUNNING
_HOGGING_DETECTION_RUNNING = True
wait = defer_to_thread(detect_hogging, 'detect-hogging')

@atexit.register
def stop_detection():
global _HOGGING_DETECTION_RUNNING
_HOGGING_DETECTION_RUNNING = False
wait()


def _patch_module_locks():
Expand Down Expand Up @@ -133,7 +144,7 @@ def mark_switch(event, args):
current_blocker_time = 0
last_warning_time = 0

while True:
while _HOGGING_DETECTION_RUNNING:
non_gevent_sleep(HOGGING_TIMEOUT)
if did_switch:
# all good
Expand Down