Add configurable throttle mechanism

This commit is contained in:
gcarq
2017-11-11 16:47:19 +01:00
parent 8f817a3634
commit d3b3370f23
4 changed files with 56 additions and 7 deletions

View File

@@ -1,11 +1,15 @@
import argparse
import enum
import logging
from typing import Any, Callable
import time
from wrapt import synchronized
from freqtrade import __version__
logger = logging.getLogger(__name__)
class State(enum.Enum):
RUNNING = 0
@@ -36,6 +40,23 @@ def get_state() -> State:
return _STATE
def throttle(func: Callable[..., Any], min_secs: float, *args, **kwargs) -> Any:
"""
Throttles the given callable that it
takes at least `min_secs` to finish execution.
:param func: Any callable
:param min_secs: minimum execution time in seconds
:return: Any
"""
start = time.time()
result = func(*args, **kwargs)
end = time.time()
duration = max(min_secs - (end - start), 0.0)
logger.debug('Throttling %s for %.2f seconds', func.__name__, duration)
time.sleep(duration)
return result
def build_arg_parser() -> argparse.ArgumentParser:
""" Builds and returns an ArgumentParser instance """
parser = argparse.ArgumentParser(
@@ -104,6 +125,12 @@ CONF_SCHEMA = {
'required': ['enabled', 'token', 'chat_id']
},
'initial_state': {'type': 'string', 'enum': ['running', 'stopped']},
'internals': {
'type': 'object',
'properties': {
'process_throttle_secs': {'type': 'number'}
}
}
},
'definitions': {
'exchange': {