take global lock while doing imports

This commit is contained in:
sqlcoder 2009-07-18 23:01:18 +01:00
parent 6a69bf512e
commit 3352f608cf
2 changed files with 70 additions and 54 deletions

View File

@ -154,31 +154,40 @@ class GuiAutoImport (threading.Thread):
# results to the same pipe. This means that self.path should be a a list of dirs # results to the same pipe. This means that self.path should be a a list of dirs
# to watch. # to watch.
if widget.get_active(): # toggled on if widget.get_active(): # toggled on
self.doAutoImportBool = True # - Does the lock acquisition need to be more sophisticated for multiple dirs?
widget.set_label(u' _Stop Autoimport ') # (see comment above about what to do if pipe already open)
if self.pipe_to_hud is None: # - Ideally we want to release the lock if the auto-import is killed by some
if os.name == 'nt': # kind of exception - is this possible?
command = "python HUD_main.py" + " " + self.settings['cl_options'] if self.settings['global_lock'].acquire(False): # returns false immediately if lock not acquired
bs = 0 # windows is not happy with line buffing here print "\nGlobal lock taken ..."
self.pipe_to_hud = subprocess.Popen(command, bufsize = bs, stdin = subprocess.PIPE, self.doAutoImportBool = True
universal_newlines=True) widget.set_label(u' _Stop Autoimport ')
else: if self.pipe_to_hud is None:
command = os.path.join(sys.path[0], 'HUD_main.py') if os.name == 'nt':
cl = [command, ] + string.split(self.settings['cl_options']) command = "python HUD_main.py" + " " + self.settings['cl_options']
self.pipe_to_hud = subprocess.Popen(cl, bufsize = 1, stdin = subprocess.PIPE, bs = 0 # windows is not happy with line buffing here
universal_newlines=True) self.pipe_to_hud = subprocess.Popen(command, bufsize = bs, stdin = subprocess.PIPE,
universal_newlines=True)
else:
command = os.path.join(sys.path[0], 'HUD_main.py')
cl = [command, ] + string.split(self.settings['cl_options'])
self.pipe_to_hud = subprocess.Popen(cl, bufsize = 1, stdin = subprocess.PIPE,
universal_newlines=True)
# Add directories to importer object. # Add directories to importer object.
for site in self.input_settings: for site in self.input_settings:
self.importer.addImportDirectory(self.input_settings[site][0], True, site, self.input_settings[site][1]) self.importer.addImportDirectory(self.input_settings[site][0], True, site, self.input_settings[site][1])
print "Adding import directories - Site: " + site + " dir: "+ str(self.input_settings[site][0]) print "Adding import directories - Site: " + site + " dir: "+ str(self.input_settings[site][0])
self.do_import() self.do_import()
interval=int(self.intervalEntry.get_text()) interval=int(self.intervalEntry.get_text())
gobject.timeout_add(interval*1000, self.do_import) gobject.timeout_add(interval*1000, self.do_import)
else:
print "auto-import aborted - global lock not available"
else: # toggled off else: # toggled off
self.settings['global_lock'].release()
self.doAutoImportBool = False # do_import will return this and stop the gobject callback timer self.doAutoImportBool = False # do_import will return this and stop the gobject callback timer
print "Stopping autoimport" print "Stopping autoimport - global lock released."
if self.pipe_to_hud.poll() is not None: if self.pipe_to_hud.poll() is not None:
print "HUD already terminated" print "HUD already terminated"
else: else:
@ -186,8 +195,6 @@ class GuiAutoImport (threading.Thread):
self.pipe_to_hud.communicate('\n') # waits for process to terminate self.pipe_to_hud.communicate('\n') # waits for process to terminate
self.pipe_to_hud = None self.pipe_to_hud = None
self.startButton.set_label(u' _Start Autoimport ') self.startButton.set_label(u' _Start Autoimport ')
#end def GuiAutoImport.startClicked #end def GuiAutoImport.startClicked

View File

@ -49,37 +49,47 @@ class GuiBulkImport():
self.importer.RunImportThreaded() self.importer.RunImportThreaded()
def load_clicked(self, widget, data=None): def load_clicked(self, widget, data=None):
# get the dir to import from the chooser # Does the lock acquisition need to be more sophisticated for multiple dirs?
self.inputFile = self.chooser.get_filename() # (see comment above about what to do if pipe already open)
if self.settings['global_lock'].acquire(False): # returns false immediately if lock not acquired
try:
print "\nGlobal lock taken ..."
# get the dir to import from the chooser
self.inputFile = self.chooser.get_filename()
# get the import settings from the gui and save in the importer # get the import settings from the gui and save in the importer
self.importer.setHandCount(int(self.spin_hands.get_text())) self.importer.setHandCount(int(self.spin_hands.get_text()))
self.importer.setMinPrint(int(self.spin_hands.get_text())) self.importer.setMinPrint(int(self.spin_hands.get_text()))
self.importer.setQuiet(self.chk_st_st.get_active()) self.importer.setQuiet(self.chk_st_st.get_active())
self.importer.setFailOnError(self.chk_fail.get_active()) self.importer.setFailOnError(self.chk_fail.get_active())
self.importer.setThreads(int(self.spin_threads.get_text())) self.importer.setThreads(int(self.spin_threads.get_text()))
self.importer.setHandsInDB(self.n_hands_in_db) self.importer.setHandsInDB(self.n_hands_in_db)
cb_model = self.cb_dropindexes.get_model() cb_model = self.cb_dropindexes.get_model()
cb_index = self.cb_dropindexes.get_active() cb_index = self.cb_dropindexes.get_active()
if cb_index: if cb_index:
self.importer.setDropIndexes(cb_model[cb_index][0]) self.importer.setDropIndexes(cb_model[cb_index][0])
else:
self.importer.setDropIndexes("auto")
sitename = self.cbfilter.get_model()[self.cbfilter.get_active()][0]
self.lab_info.set_text("Importing")
self.importer.addBulkImportImportFileOrDir(self.inputFile, site = sitename)
self.importer.setCallHud(False)
starttime = time()
(stored, dups, partial, errs, ttime) = self.importer.runImport()
ttime = time() - starttime
if ttime == 0:
ttime = 1
print 'GuiBulkImport.import_dir done: Stored: %d \tDuplicates: %d \tPartial: %d \tErrors: %d in %s seconds - %d/sec'\
% (stored, dups, partial, errs, ttime, stored / ttime)
self.importer.clearFileList()
self.lab_info.set_text("Import finished")
except:
pass
self.settings['global_lock'].release()
else: else:
self.importer.setDropIndexes("auto") print "bulk-import aborted - global lock not available"
sitename = self.cbfilter.get_model()[self.cbfilter.get_active()][0]
self.lab_info.set_text("Importing")
self.importer.addBulkImportImportFileOrDir(self.inputFile, site = sitename)
self.importer.setCallHud(False)
starttime = time()
(stored, dups, partial, errs, ttime) = self.importer.runImport()
ttime = time() - starttime
if ttime == 0:
ttime = 1
print 'GuiBulkImport.import_dir done: Stored: %d \tDuplicates: %d \tPartial: %d \tErrors: %d in %s seconds - %d/sec'\
% (stored, dups, partial, errs, ttime, stored / ttime)
self.importer.clearFileList()
self.lab_info.set_text("Import finished")
def get_vbox(self): def get_vbox(self):
"""returns the vbox of this thread""" """returns the vbox of this thread"""
@ -88,8 +98,7 @@ class GuiBulkImport():
def __init__(self, settings, config): def __init__(self, settings, config):
self.settings = settings self.settings = settings
self.config = config self.config = config
self.importer = fpdb_import.Importer(self, self.settings, self.importer = fpdb_import.Importer(self, self.settings, config)
config)
self.vbox = gtk.VBox(False, 0) self.vbox = gtk.VBox(False, 0)
self.vbox.show() self.vbox.show()