Public Access
Improved interruptable sleep and warn user when using time.sleep.
This commit is contained in:
@@ -3,6 +3,7 @@ import io
|
||||
import traceback
|
||||
import sys
|
||||
import time
|
||||
import ast
|
||||
from damaris.experiments.Experiment import Quit
|
||||
from damaris.experiments import Experiment
|
||||
|
||||
@@ -48,8 +49,32 @@ class ExperimentHandling(threading.Thread):
|
||||
self.raised_exception = None
|
||||
self.location = None
|
||||
exp_iterator=None
|
||||
|
||||
# check for time.sleep()
|
||||
try:
|
||||
tree = ast.parse(self.script)
|
||||
for node in ast.walk(tree):
|
||||
found_blocking_sleep = False
|
||||
if isinstance(node, ast.Call):
|
||||
func = node.func
|
||||
if isinstance(func, ast.Attribute) and func.attr == 'sleep':
|
||||
if isinstance(func.value, ast.Name) and func.value.id == 'time':
|
||||
found_blocking_sleep = True
|
||||
elif isinstance(node, ast.ImportFrom) and node.module == 'time':
|
||||
for alias in node.names:
|
||||
if alias.name == 'sleep':
|
||||
found_blocking_sleep = True
|
||||
|
||||
if found_blocking_sleep:
|
||||
print("Warning: time.sleep() detected in experiment script. This is blocking and not interruptible. Please use sleep() instead.")
|
||||
break
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
exec(self.script, dataspace)
|
||||
except StopExperiment:
|
||||
return
|
||||
except Exception as e:
|
||||
self.raised_exception=e
|
||||
self.location=traceback.extract_tb(sys.exc_info()[2])[-1][1:3]
|
||||
@@ -61,6 +86,8 @@ class ExperimentHandling(threading.Thread):
|
||||
if "experiment" in dataspace:
|
||||
try:
|
||||
exp_iterator=dataspace["experiment"]()
|
||||
except StopExperiment:
|
||||
return
|
||||
except Exception as e:
|
||||
self.raised_exception=e
|
||||
self.location=traceback.extract_tb(sys.exc_info()[2])[-1][1:3]
|
||||
|
||||
Reference in New Issue
Block a user