def __init__(self, name=None): (DEBUG and name) and Log.note("New signal {{name|quote}}", name=name) self._name = name self.lock = _allocate_lock() self._go = False self.job_queue = None self.waiting_threads = None
def query(self, command): """ WILL BLOCK CALLING THREAD UNTIL THE command IS COMPLETED :param command: COMMAND FOR SQLITE :return: list OF RESULTS """ if self.closed: Log.error("database is closed") signal = _allocate_lock() signal.acquire() result = Data() trace = get_stacktrace(1) if self.get_trace else None if self.get_trace: current_thread = Thread.current() with self.locker: for t in self.available_transactions: if t.thread is current_thread: Log.error(DOUBLE_TRANSACTION_ERROR) self.queue.add(CommandItem(command, result, signal, trace, None)) signal.acquire() if result.exception: Log.error("Problem with Sqlite call", cause=result.exception) return result
def __init__(self, name=None): (DEBUG and name) and Log.note("New signal {{name|quote}}", name=name) self._name = name self.lock = _allocate_lock() self._go = False self.job_queue = None self.waiting_threads = None
def __init__(self, name="", debug=DEBUG, sample=False): if (debug or sample) and not _Log: _late_import() self.debug = debug self.sample = sample self.name = name self.lock = _allocate_lock() self.waiting = None
def __init__(self, signal, count): """ CALL signal.go() WHEN done() IS CALLED count TIMES :param signal: :param count: :return: """ self.signal = signal self.locker = _allocate_lock() self.remaining = count
def __init__(self, signal, count): """ CALL signal.go() WHEN done() IS CALLED count TIMES :param signal: :param count: :return: """ self.signal = signal self.locker = _allocate_lock() self.remaining = count
def commit(self): """ WILL BLOCK CALLING THREAD UNTIL ALL PREVIOUS execute() CALLS ARE COMPLETED :return: """ if self.closed: Log.error("database is closed") signal = _allocate_lock() signal.acquire() self.queue.add((COMMIT, None, signal, None)) signal.acquire() return
def test_lock_speed(self): SCALE = 1000 * 100 with Timer("create"): locks = [_allocate_lock() for _ in range(SCALE)] with Timer("acquire"): for i in range(SCALE): locks[i].acquire() with Timer("release"): for i in range(SCALE): locks[i].release()
def query(self, query): if self.db.closed: Log.error("database is closed") signal = _allocate_lock() signal.acquire() result = Data() trace = get_stacktrace(1) if self.db.get_trace else None self.db.queue.add(CommandItem(query, result, signal, trace, self)) signal.acquire() if result.exception: Log.error("Problem with Sqlite call", cause=result.exception) return result
def close(self): """ OPTIONAL COMMIT-AND-CLOSE IF THIS IS NOT DONE, THEN THE THREAD THAT SPAWNED THIS INSTANCE :return: """ self.closed = True signal = _allocate_lock() signal.acquire() self.queue.add(CommandItem(COMMIT, None, signal, None, None)) signal.acquire() self.worker.please_stop.go() return
class Till(Signal): """ TIMEOUT AS A SIGNAL """ __slots__ = [] locker = _allocate_lock() next_ping = time() new_timers = [] def __new__(cls, till=None, seconds=None): if not enabled: Log.note("Till daemon not enabled") return DONE elif till != None: return object.__new__(cls) elif seconds == None: return object.__new__(cls) elif seconds <= 0: return DONE else: return object.__new__(cls) def __init__(self, till=None, seconds=None): """ Signal after some elapsed time: Till(seconds=1).wait() :param till: UNIX TIMESTAMP OF WHEN TO SIGNAL :param seconds: PREFERRED OVER timeout """ now = time() if till != None: if not isinstance(till, (float, int)): from mo_logs import Log Log.error("Date objects for Till are no longer allowed") timeout = till elif seconds != None: timeout = now + seconds else: from mo_logs import Log raise Log.error("Should not happen") Signal.__init__(self, name=text(timeout)) with Till.locker: if timeout != None: Till.next_ping = min(Till.next_ping, timeout) Till.new_timers.append(TodoItem(timeout, ref(self)))
class Till(Signal): """ TIMEOUT AS A SIGNAL """ __slots__ = [] locker = _allocate_lock() next_ping = time() done = Signal("Timers shutdown") enabled = False new_timers = [] def __new__(cls, till=None, timeout=None, seconds=None): if not Till.enabled: return Till.done elif till == None and timeout == None and seconds == None: return None else: return object.__new__(cls) def __init__(self, till=None, timeout=None, seconds=None): now = time() if till != None: if not isinstance(till, (float, int)): from mo_logs import Log Log.error("Date objects for Till are no longer allowed") timeout = till elif seconds != None: timeout = now + seconds elif timeout != None: if not isinstance(timeout, (float, int)): from mo_logs import Log Log.error("Duration objects for Till are no longer allowed") timeout = now + timeout else: from mo_logs import Log Log.error("Should not happen") Signal.__init__(self, name=text_type(timeout)) with Till.locker: if timeout != None: Till.next_ping = min(Till.next_ping, timeout) Till.new_timers.append((timeout, ref(self)))
def query(self, command): """ WILL BLOCK CALLING THREAD UNTIL THE command IS COMPLETED :param command: COMMAND FOR SQLITE :return: list OF RESULTS """ if self.closed: Log.error("database is closed") if not self.worker: self.worker = Thread.run("sqlite db thread", self._worker) signal = _allocate_lock() signal.acquire() result = Data() self.queue.add((command, result, signal, None)) signal.acquire() if result.exception: Log.error("Problem with Sqlite call", cause=result.exception) return result
def wait(self): """ PUT THREAD IN WAIT STATE UNTIL SIGNAL IS ACTIVATED """ if self._go: return True with self.lock: if self._go: return True stopper = _allocate_lock() stopper.acquire() if not self.waiting_threads: self.waiting_threads = [stopper] else: self.waiting_threads.append(stopper) DEBUG and self._name and Log.note("wait for go {{name|quote}}", name=self.name) stopper.acquire() DEBUG and self._name and Log.note("GOing! {{name|quote}}", name=self.name) return True
def wait(self): """ PUT THREAD IN WAIT STATE UNTIL SIGNAL IS ACTIVATED """ if self._go: return True with self.lock: if self._go: return True stopper = _allocate_lock() stopper.acquire() if not self.waiting_threads: self.waiting_threads = [stopper] else: self.waiting_threads.append(stopper) DEBUG and self._name and Log.note("wait for go {{name|quote}}", name=self.name) stopper.acquire() DEBUG and self._name and Log.note("GOing! {{name|quote}}", name=self.name) return True
def __init__(self, name=""): if DEBUG and not _Log: _late_import() self.name = name self.lock = _allocate_lock() self.waiting = None