# This file is part of ReportTool # ReportTool (Felicity) is copyright 2004-8 Steve Butterfill. # # ReportTool is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # ReportTool is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with ReportTool. If not, see . # # If you want to use ReportTool under a difference licence, email # s.butterfill@warwick.ac.uk. #!/usr/bin/env python # -------------------------- # threads for uploading # partly based on # http://groups.google.com/group/turbogears/browse_thread/thread/2be56c7df244f837/98eb9e9b4efa36a # -------------------------- import sys, threading, traceback from felicity.hacks import safe_str from felicity.model import hub, Job, UploadDataSet from sqlobject.util.threadinglocal import local as threading_local #global lock -- only one upload runs at once upload_mutex = threading.Lock() def start_upload_thread(file, delimiter, dataset, upload_function, job): """threaded version of omr upload """ dataset_id = None if dataset is not None: dataset_id = dataset.id ult = UploadThread(file=file, delimiter=delimiter, dataset_id=dataset_id, upload_function = upload_function, job_id=job.id) job.update_progress("Waiting in queue for processing to start.") ult.start() class UploadThread(threading.Thread): def __init__(self, file, delimiter, dataset_id, upload_function, job_id): self.file = file self.delimiter = delimiter self.dataset_id = dataset_id self.upload_function = upload_function self.job_id = job_id threading.Thread.__init__(self) def run(self): # This part is important, it gives us a new connection for this # thread, otherwise your thread will get messy db access try: hub.threadingLocal = threading_local() hub.begin() job = Job.get(self.job_id) dataset = None if self.dataset_id is not None: dataset = UploadDataSet.get(self.dataset_id) upload_mutex.acquire() result = self.upload_function(file=self.file, delimiter=self.delimiter, dataset=dataset, job=job) except: try: exc_info = sys.exc_info() error_msg = traceback.format_exception(*exc_info) hub.rollback() hub.begin() job.failed(safe_str(error_msg)) hub.commit() raise exc_info[0], exc_info[1], exc_info[2] except: print >> sys.stderr, "Error in UploadThread exception handler" traceback.print_exc() else: hub.commit() hub.begin() job.done(result) hub.commit() upload_mutex.release()