def run(self): try: self.hdoj_last_submission_id = self.request_last_submission()[0][0] #print "[Task %s]: Last ID: %s" % (self.submission.id, self.hdoj_last_submission_id) if self.submit() == False: raise Exception("Error: Submit failed.") while self.stillRuning: current_sub = self.request_last_submission() if self.hdoj_last_submission_id == current_sub[0][0]: time.sleep(2) continue if self.stillRuning == False: raise Exception("thread has already been killed.") if current_sub[0][1] != 'Queuing' and current_sub[0][1] != 'Running' and current_sub[0][1] != 'Compiling': session = db.create_scoped_session() session.execute("UPDATE submission set result='%s', judger_status=-1, memory_used='%s', time_used='%s', original_oj_submit_id=%s WHERE id=%d" % (current_sub[0][1],current_sub[0][3],current_sub[0][2],current_sub[0][0],self.submission.id)) session.flush() session.commit() self.done = True break session = db.create_scoped_session() session.execute("UPDATE submission set result='%s' WHERE id=%d" % (current_sub[0][1],self.submission.id)) session.flush() if self.stillRuning == False: raise Exception("thread has already been killed.") session.commit() except SQLAlchemyError, e: print "Warning: HDOJ.run()" print "rollback" db.session.rollback() print e
def setUp(self): create_app("test") from application import app, db self.app = app with app.test_request_context(): db.create_all() # Initialize database, if needed self.db_session = db.create_scoped_session()
def daemon(th, timeout, account): try: th.setDaemon(True) th.start() th.join(timeout) th.stillRuning=False if th.done == False: print "[Task #%s]: rejudge."% th.submission.id session = db.create_scoped_session() session.execute("update submission set judger_status=0 where id=%d" % th.submission.id) session.flush() session.commit() #print "[Task #%s]: daemon killed or stoped." % th.submission.id account['used'] = False except Exception, e: print "Warning: __init__.daemon()" print e
def request_new_submission_by_databse(self): try: OJs = "" for x in self.judgers: if self.request_spare_judger(str(x)) != None: if OJs != "": OJs = OJs + " or " OJs = OJs + ("original_oj='%s'" % str(x)) if OJs == "": return None session = db.create_scoped_session() submission = session.execute("select * from submission where judger_status=0 and (%s) limit 1"%OJs).first() if submission is not None: session.execute("update submission set judger_status=%d where id=%d" % (time.time(), submission.id)) session.flush() session.commit() return submission except SQLAlchemyError, e: print "Warning: __init__.request_new_submission_by_databse()" print "rollback" db.session.rollback() print e return None
def save_image_batch(self): session = db.create_scoped_session( ) # create a new DB session for this thread categories = session.query(Category).all() categories_map = {} for c in categories: categories_map[c.label] = c while True: batch = self.qImgData.get() start = time.time() for path, unit_feat, mag, cat_id, score in zip( batch.paths, batch.unit_features, batch.magnitudes, batch.category_ids, batch.scores): new_img = Image(path=path, unit_features=utils.adapt_array(unit_feat), magnitude=mag.item(), category=categories_map[cat_id], score=score.item()) session.add(new_img) session.commit() end = time.time() logging.debug("Queue size: " + str(self.qImgData.qsize())) logging.debug("Batch saved in database: " + str(end - start) + "s") self.qImgData.task_done()