Skip to content

Commit

Permalink
Add a callback to run_in_background
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreDecan committed Dec 19, 2015
1 parent 90249b3 commit ab32f07
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion sismic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from . import evaluator, io, model, interpreter, stories

__description__ = 'Sismic Interactive State Machine Interpreter and Checker'
__version__ = '0.11.2'
__version__ = '0.11.3'
__url__ = 'https://github.com/AlexandreDecan/sismic/'
__author__ = 'Alexandre Decan'
__email__ = 'alexandre.decan@lexpage.net'
Expand Down
7 changes: 5 additions & 2 deletions sismic/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,13 +553,14 @@ def __repr__(self):
return '{}[{}]({})'.format(self.__class__.__name__, self._statechart, ', '.join(self.configuration))


def run_in_background(interpreter: Interpreter, delay: float=0.2):
def run_in_background(interpreter: Interpreter, delay: float=0.2, callback=None):
"""
Run given interpreter in background. The time is updated according to
``time.time()``. The interpreter is ran until it reachs a final configuration.
:param interpreter: an interpreter
:param delay: delay between each call to ``execute()``
:param callback: a function that takes the interpreter and the results of ``execute``.
:return: started thread
"""
import time
Expand All @@ -569,7 +570,9 @@ def _task(interpreter, delay):
starttime = time.time()
while not interpreter.final:
interpreter.time = time.time() - starttime
interpreter.execute()
steps = interpreter.execute()
if callback:
callback(interpreter, steps)
time.sleep(delay)
thread = threading.Thread(target=_task, args=(interpreter, delay))
thread.start()
Expand Down

0 comments on commit ab32f07

Please sign in to comment.