Public Access
Improved interruptable sleep and warn user when using time.sleep.
This commit is contained in:
1 parent
a3738025b7
commit
1c4f8f5344
3 files changed
+65
-6
No files matched your search
@@ -4,7 +4,9 @@ import sys
|
||||
import os
|
||||
import os.path
|
||||
import traceback
|
||||
import ast
|
||||
from damaris.data import Resultable
|
||||
from damaris.gui.ExperimentHandling import StopExperiment
|
||||
|
||||
class ResultHandling(threading.Thread):
|
||||
"""
|
||||
@@ -30,10 +32,35 @@ class ResultHandling(threading.Thread):
|
||||
del data_classes
|
||||
dataspace["results"]=self
|
||||
dataspace["data"]=self.data_space
|
||||
dataspace["sleep"]=self.sleep
|
||||
self.raised_exception=None
|
||||
self.location = 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 result 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]
|
||||
@@ -47,6 +74,8 @@ class ResultHandling(threading.Thread):
|
||||
return
|
||||
try:
|
||||
dataspace["result"]()
|
||||
except StopExperiment:
|
||||
pass
|
||||
except Exception as e:
|
||||
self.raised_exception=e
|
||||
self.location=traceback.extract_tb(sys.exc_info()[2])[-1][1:3]
|
||||
@@ -56,6 +85,11 @@ class ResultHandling(threading.Thread):
|
||||
traceback_file=None
|
||||
dataspace=None
|
||||
|
||||
def sleep(self, seconds):
|
||||
self.quit_flag.wait(seconds)
|
||||
if self.quit_flag.isSet():
|
||||
raise StopExperiment
|
||||
|
||||
def __iter__(self):
|
||||
if self.quit_flag.isSet():
|
||||
self.results=None
|
||||
|
||||
Reference in new issue
Block a user