def init(self): super(InsertWorker, self).init() from uliweb import settings from uliweb.orm import Reset, get_session patch_mysqldb() get_session(create=True) # Reset() # table = reflect_table(self._table) para = dict(zip(self.fields, [''] * len(self.fields))) self.sql = str(self.table.insert().values(para)) # self.sql = self.model.table.insert() Begin()
def init(self): super(InsertWorker, self).init() from uliweb import settings from uliweb.orm import Reset, get_session patch_mysqldb() get_session(create=True) # Reset() # table = reflect_table(self._table) para = dict(zip(self.fields, ['']*len(self.fields))) self.sql = str(self.table.insert().values(para)) # self.sql = self.model.table.insert() Begin()
def __call__(self, func, *args, **kwargs): from uliweb import orm self.clear() if len(self.pids) == self.size: pid, status = os.wait() if pid in self.pids: del self.pids[pid] pid = os.fork() if pid == 0: orm.get_session(create=True) orm.Begin() try: func(*args, **kwargs) orm.Commit() except: orm.Rollback() orm.get_session().close() os._exit(0) else: self.pids[pid] = func, args, kwargs
def _f(table, filename, msg): session = orm.get_session() if session: session.close() orm.Begin() try: result = load_table(table, filename, engine, delimiter=options.delimiter, format=format, encoding=options.encoding, delete=ans=='Y', bulk=int(options.bulk), engine_name=engine.engine_name) if global_options.verbose: print msg, result orm.Commit() except: orm.Rollback()
def _f(table, filename, msg): session = orm.get_session() if session: session.close() orm.Begin() try: result = load_table(table, filename, engine, delimiter=options.delimiter, format=format, encoding=options.encoding, delete=ans=='Y', bulk=int(options.bulk), engine_name=engine.engine_name) if global_options.verbose: print(msg, result) orm.Commit() except: orm.Rollback()
def call_async(commands, parameters=None, direct=True, auto_commit=True, session=None): """ Submit an async task to database and queue. :param command: command could be a dict or a string or a function object :param direct: if True, it'll invoke underline worker api to execute the task :param auto_commit: if True, then it'll create new session to commit, False will use current session object or passed session object, if current session is not in transaction, it'll automatically start transaction :param session: Only used for auto_commit is False :return: task_id value Several invoke format: call_async({'cmd_name':'echo', 'parameters':...}) or c = AsyncCommand('echo', parameters=...) call_async(c) """ from uliweb.orm import get_session, Session from .daemon import process_task if auto_commit: session = Session() else: session = session or get_session() trans = False if auto_commit: session.begin() trans = True else: if not session.in_transaction(): session.begin() trans = True try: if isinstance(commands, AsyncCommand): commands = [commands] elif isinstance(commands, AsyncCommandList): commands = commands.tasks elif not isinstance(commands, (list, tuple)): commands = [AsyncCommand(commands, kwargs=parameters)] #根据条数发送若干通知 if direct: def f(n=len(commands)): redis = functions.get_redis() redis.lpush('async_tasks_beat', *['start']*n) session.post_commit_once = f tasks = [] for c in commands: if not isinstance(c, AsyncCommand): msg = "Wrong command type, but %r type found" % c log.error(msg) raise ErrorWrongCommandType(msg) tasks.append(c.commit(session)) if trans: session.commit() except: if trans: session.rollback() raise