def execute_function(fname, args, kwargs, job_id, queue): from sqlalchemy import create_engine import locjoin.settings as settings import locjoin.meta as meta from locjoin import init_model db = create_engine(settings.DBURI, isolation_level='REPEATABLE READ') init_model(db) try: strargs = pickle.dumps([args, kwargs]) task = Task.check_and_add(session, fname, strargs) if not task: raise except: traceback.print_exc() queue.put(None) queue.put((False, job, traceback.format_exc())) return tid = task.id queue.put(tid) try: if fname == 'wait': print "wait", job_id time.sleep(args[0]) print "wait done", job_id elif fname == 'crash': time.sleep(2) raise RuntimeError("crashed") else: cmd = '_%s(db, meta.session, *args, **kwargs)' % fname exec(cmd) queue.put((True, job_id, '')) except: err = traceback.format_exc() queue.put((False, job_id, err)) finally: session.close() db.pool.dispose() Task.delete(session, tid)
from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy from sqlalchemy import * from sqlalchemy.orm import sessionmaker from settings import DBURI from locjoin import init_model app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = DBURI db = SQLAlchemy(app) setattr(db, 'execute', db.engine.execute) init_model(db.engine) import dbtrucksite.views
def wait(session, secs=10): add_job(session, "wait", [secs], {}) def crash(session, *args): add_job(session, "crash", args, {}) def test_insert(session, v): add_job(session, "test_insert", [v], {}) def test_inc(session, v): add_job(session, "test_inc", [v], {}) if __name__ == "__main__": import locjoin.settings as settings from locjoin import init_model import locjoin.meta as meta db = create_engine(settings.DBURI, isolation_level="REPEATABLE READ") init_model(db) check_job_table(db) run_extractor(meta.session, "realestate_small") # for i in xrange(11, 50, 1): # test_inc(meta.session, i)