Implemented ordered queue of experiments for starting multiple DAMARIS

instances simultaneously.
This commit is contained in:
Markus Rosenstihl 2018-03-23 12:26:21 +01:00
parent 58515f1cf3
commit 113cc82684
2 changed files with 71 additions and 48 deletions

View File

@ -4,7 +4,7 @@
# this must happen before any damaris stuff is called!
import sys
import os, argparse
import sqlite3
# for numpy-1.1 and later: check the environment for LANG and LC_NUMERIC
# see: http://projects.scipy.org/scipy/numpy/ticket/902
if os.environ.get("LANG", "").startswith("de") or os.environ.get("LC_NUMERIC", "").startswith("de"):
@ -13,22 +13,36 @@ if os.environ.get("LANG", "").startswith("de") or os.environ.get("LC_NUMERIC", "
parser = argparse.ArgumentParser(description='DArmstadt MAgnetic Resonance Instrumentation Software')
parser.add_argument("--run", action="store_true", help="run DAMARIS immediately with given scripts")
parser.add_argument("--clean", action="store_true", help="cleanup DAMARIS run files")
parser.add_argument("--debug", action="store_true", help="run DAMARIS with DEBUG flag set")
parser.add_argument("--mpl", help="run DAMARIS with matplotlib backend",
choices=["GTKAgg","GTKCairo","GTK"], default="GTKAgg")
parser.add_argument("exp_script", help="experiment script", nargs="?", metavar="EXP.py")
parser.add_argument("res_script", help="result script", nargs="?", metavar="RES.py")
#parser.add_argument("exp_res_script", help="(NOT IMPLEMENTED) further experiment and result script pairs", nargs=argparse.REMAINDER, metavar="EXPn.py RESn.py")
args = parser.parse_args()
import matplotlib
if args.mpl:
matplotlib.use(args.mpl)
import damaris.gui.DamarisGUI
lockfile = os.path.expanduser('~/.damaris.lockdb')
if args.clean:
if os.path.exists(lockfile):
os.remove(lockfile)
lockdb = sqlite3.connect(lockfile)
c = lockdb.cursor()
c.execute("CREATE TABLE IF NOT EXISTS damaris (uuid text, status text)")
print "Make sure experiment table exists"
lockdb.commit()
if args.debug:
damaris.gui.DamarisGUI.debug = True
print "debug flag set"
@ -38,9 +52,17 @@ if args.debug:
except ImportError:
pass
matplotlib.rcParams["verbose.level"] = "debug"
d = damaris.gui.DamarisGUI.DamarisGUI(args.exp_script, args.res_script, start_immediatly=args.run)
print args
d = damaris.gui.DamarisGUI.DamarisGUI(args.exp_script, args.res_script, start_immediately=args.run)
print "run"
d.run()
#for exp_script, res_script in args.exp_res_script:
# print "here"
# d = damaris.gui.DamarisGUI.DamarisGUI(exp_script, res_script, start_immediatly=args.run)
# d.run()
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
#lockdb.close()

View File

@ -18,7 +18,7 @@ import xdg.BaseDirectory
# import 3rd party modules
# gui graphics
import gobject
import glob
import sqlite3,uuid
gobject.threads_init( )
import pygtk
@ -119,25 +119,36 @@ DataPool.log = log
class LockFile:
def __init__(self):
self.index = 0
self.templ = os.path.expanduser("~/.damaris.lock")
self.existing = []
self._lockfile = self.lockfile(self.index)
self.has_lockfile = None
self.file = os.path.expanduser("~/.damaris.lockdb")
self.conn = sqlite3.connect(self.file)
self.cursor = self.conn.cursor()
self.id = None
print "Connected to db.."
def add_experiment(self):
print "Add experiment.."
self.id = str(uuid.uuid4())
self.cursor.execute("INSERT INTO damaris VALUES (?, ?)",(self.id, "started"))
self.conn.commit()
return self.id
def del_experiment(self, id):
print "Delete experiment.."
self.cursor.execute('DELETE FROM damaris WHERE uuid=?', (self.id,))
self.conn.commit()
return True
def am_i_next(self):
first = self.cursor.execute("SELECT * FROM damaris ORDER BY ROWID ASC LIMIT 1")
current_running = first.fetchone()[0]
#print current_running,self.id
if current_running == self.id:
return True
else:
return False
def lockfile(self, idx):
return "%s.%i"%(self.templ, idx)
def get_lockfile(self):
#self._lockfile = self.lockfile()
while os.path.exists(self._lockfile):
self.existing.append(self._lockfile)
self.index += 1
self._lockfile = self.lockfile(self.index)
print self._lockfile
self.has_lockfile = self._lockfile
def no_lockfiles(self):
return glob.glob("%s.*"%self.templ) == []
class DamarisGUI:
@ -153,7 +164,7 @@ class DamarisGUI:
Stop_State = 3
Quit_State = 4
def __init__( self, exp_script_filename=None, res_script_filename=None, start_immediatly=False ):
def __init__(self, exp_script_filename=None, res_script_filename=None, start_immediately=False):
# state: edit, run, stop, quit
# state transitions:
# edit -> run|quit
@ -183,6 +194,7 @@ class DamarisGUI:
self.config = ConfigTab( self.xml_gui )
# lock file to prevent other DAMARIS to start immediatly
self.lockfile = LockFile() # = os.path.expanduser("~/.damaris.lock")
self.id = None
exp_script = u""
if exp_script_filename is not None and exp_script_filename != "":
@ -197,14 +209,12 @@ class DamarisGUI:
res_script = self.sw.load_file_as_unicode( res_script_filename )
self.sw.set_scripts( exp_script, res_script )
if start_immediatly:
if start_immediately:
if exp_script and res_script:
self.start_immediatly = start_immediatly
self.start_immediatly = start_immediately
else:
raise ("RuntimeError", "experiment and result scripts not given,\
aborting immediate start of experiment")
else:
self.start_immediatly = False
self.statusbar_init( )
@ -436,20 +446,18 @@ class DamarisGUI:
# set the text mark for hdf logging
if self.log.textbuffer.get_mark( "lastdumped" ) is None:
self.log.textbuffer.create_mark( "lastdumped", self.log.textbuffer.get_end_iter( ), left_gravity=True )
# only observe lock file if you want to start immediatly after start up
if self.start_immediatly:
loop_run=0
interval = 2
self.lockfile.get_lockfile()
while not self.lockfile.no_lockfiles():
time.sleep(interval)
while gtk.events_pending():
gtk.main_iteration(False)
if loop_run > 2:
self.experiment_script_statusbar_label.set_text("Waiting for other experiment to finish (My lockfile: %s)" % self.lockfile.has_lockfile)
loop_run = 0
print "Lockfile still exists, waiting ... (%s)" % self.lockfile.has_lockfile
loop_run += interval
loop_run=0
interval = 2
self.id = self.lockfile.add_experiment()
while not self.lockfile.am_i_next():
time.sleep(interval)
while gtk.events_pending():
gtk.main_iteration(False)
if loop_run > 1:
self.experiment_script_statusbar_label.set_text("Waiting for other experiment to finish")
loop_run = 0
loop_run += interval
# start experiment
try:
@ -500,9 +508,6 @@ class DamarisGUI:
# switch to grapics
self.main_notebook.set_current_page( DamarisGUI.Monitor_Display )
# create lock file
print "Creating lockfile %s"%self.lockfile.has_lockfile
lfile = open(self.lockfile.has_lockfile, "w").close()
# set running
if self.si.exp_handling is not None:
self.experiment_script_statusbar_label.set_text( "Experiment Script Running (0)" )
@ -650,11 +655,7 @@ class DamarisGUI:
# keep data to display but throw away everything else
self.si = None
# delete locak file so that other experiment can start
if os.path.exists(self.lockfile.has_lockfile):
try:
os.remove(self.lockfile.has_lockfile)
except:
raise IOError("Could not remove lock file: %s"%self.lockfile)
self.lockfile.del_experiment(self.id)
return False
# dump states?