Implemented ordered queue of experiments for starting multiple DAMARIS
instances simultaneously.
This commit is contained in:
@@ -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?
|
||||
|
Reference in New Issue
Block a user