-
Notifications
You must be signed in to change notification settings - Fork 88
Gavrenkov/gunicorn gevent runtime #1644
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
Draft
s-gavrenkov
wants to merge
14
commits into
master
Choose a base branch
from
gavrenkov/gunicorn_gevent_runtime
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5f4604c
add files
s-gavrenkov ed4404f
fixed logs
s-gavrenkov 1b6ff68
fix run
s-gavrenkov 850bd9c
add runtime for flask
s-gavrenkov 188db51
removed args
s-gavrenkov 3c872bd
fix main
s-gavrenkov 45dd03d
Merge remote-tracking branch 'origin/master' into gavrenkov/gunicorn_…
s-gavrenkov 651d7fc
lint formatting
s-gavrenkov 2a69a45
worker class
s-gavrenkov 5a65d31
fixed monkey
s-gavrenkov cf4aad4
fixed monkey
s-gavrenkov 87e9840
corrected
s-gavrenkov f543123
add file
s-gavrenkov 799bbc4
add self._terminate
s-gavrenkov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
custom_model_runner/datarobot_drum/drum/root_predictors/gevent_stdout_flusher.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import logging | ||
import sys | ||
import time | ||
try: | ||
import gevent | ||
from gevent import Greenlet | ||
HAS_GEVENT = True | ||
except ImportError: | ||
HAS_GEVENT = False | ||
import threading | ||
|
||
HAS_GEVENT = True | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
class GeventCompatibleStdoutFlusher: | ||
"""An implementation to flush the stdout after a certain time of no activity. | ||
Compatible with both gevent and threading environments.""" | ||
|
||
def __init__(self, max_time_until_flushing=1.0): | ||
self._max_time_until_flushing = max_time_until_flushing | ||
self._last_predict_time = None | ||
self._flusher_greenlet = None | ||
self._flusher_thread = None | ||
self._stop_event = None | ||
self._running = False | ||
|
||
def start(self): | ||
"""Start the stdout flusher.""" | ||
if self._running: | ||
return | ||
|
||
self._running = True | ||
|
||
if HAS_GEVENT: | ||
self._flusher_greenlet = gevent.spawn(self._flush_greenlet_method) | ||
else: | ||
self._stop_event = threading.Event() | ||
self._flusher_thread = threading.Thread(target=self._flush_thread_method) | ||
"""Check if the stdout flusher is alive.""" | ||
if HAS_GEVENT and self._flusher_greenlet: | ||
return not self._flusher_greenlet.dead | ||
elif self._flusher_thread: | ||
return self._flusher_thread.is_alive() | ||
return False | ||
|
||
def stop(self): | ||
"""Stop the flusher in a synchronous fashion.""" | ||
if not self._running: | ||
logging.error("Flusher thread stopped 1.") | ||
return | ||
|
||
self._running = False | ||
logging.error("Flusher thread stopped 2.") | ||
if HAS_GEVENT and self._flusher_greenlet: | ||
self._flusher_greenlet.kill() | ||
self._flusher_greenlet = None | ||
elif self._flusher_thread and self._stop_event: | ||
logging.error("Flusher thread stopped 3.") | ||
self._stop_event.set() | ||
self._flusher_thread.join(timeout=2.0) # Timeout to prevent hanging | ||
self._flusher_thread = None | ||
self._stop_event = None | ||
logging.error("Flusher thread stopped 4.") | ||
|
||
def set_last_activity_time(self): | ||
"""Set the last activity time that will be used as the reference for time comparison.""" | ||
self._last_predict_time = self._current_time() | ||
|
||
@staticmethod | ||
def _current_time(): | ||
return time.time() | ||
|
||
def _flush_greenlet_method(self): | ||
"""Gevent greenlet method for stdout flushing""" | ||
try: | ||
while self._running: | ||
self._process_stdout_flushing() | ||
#gevent.sleep(self._max_time_until_flushing) | ||
gevent.sleep(0) | ||
except gevent.GreenletExit: | ||
pass # Normal termination | ||
|
||
def _flush_thread_method(self): | ||
"""Threading method for stdout flushing""" | ||
while self._running and not self._stop_event.wait(self._max_time_until_flushing): | ||
self._process_stdout_flushing() | ||
|
||
def _process_stdout_flushing(self): | ||
if self._is_predict_time_set_and_max_waiting_time_expired(): | ||
sys.stdout.flush() | ||
sys.stderr.flush() | ||
|
||
def _is_predict_time_set_and_max_waiting_time_expired(self): | ||
if self._last_predict_time is None: | ||
return False | ||
|
||
return (self._current_time() - self._last_predict_time) >= self._max_time_until_flushing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably this can be removed as we require gunicorn
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe import can be even moved to the beginning of the file