def test_queue_speed(self): SCALE = 1000*10 done = Signal("done") slow = Queue() q = ThreadedQueue("test queue", queue=slow) def empty(please_stop): while not please_stop: item = q.pop() if item is Thread.STOP: break done.go() Thread.run("empty", empty) timer = Timer("add {{num}} to queue", param={"num": SCALE}) with timer: for i in range(SCALE): q.add(i) q.add(Thread.STOP) Log.note("Done insert") done.wait() self.assertLess(timer.duration.seconds, 1.5, "Expecting queue to be fast")
def __init__(self, till=None, timeout=None, seconds=None): global next_ping Signal.__init__(self, "a timeout") if till != None: timeout = Date(till).unix elif timeout != None: timeout = _time() + Duration(timeout).seconds elif seconds != None: timeout = _time() + seconds with Till.locker: next_ping = min(next_ping, timeout) Till.all_timers.append((timeout, self))
def __init__(self, till=None, timeout=None, seconds=None): global next_ping if till != None: timeout = Date(till).unix elif seconds != None: timeout = time() + seconds elif timeout != None: timeout = time() + Duration(timeout).seconds Signal.__init__(self, name=unicode(timeout)) with _till_locker: next_ping = min(next_ping, timeout) Till.new_timers.append((timeout, self))
def test_loop(self): acc = [] started = Signal() def work(please_stop): started.go() while not please_stop: acc.append(Date.now().unix) Till(seconds=0.1).wait() worker = Thread.run("loop", work) started.wait() while len(acc)<10: Till(seconds=0.1).wait() worker.stop() worker.join() # We expect 10, but 9 is good enough num = len(acc) self.assertGreater(num, 9, "Expecting some reasonable number of entries to prove there was looping, not "+unicode(num))
def wait(self, till=None): """ THE ASSUMPTION IS wait() WILL ALWAYS RETURN WITH THE LOCK ACQUIRED :param till: WHEN TO GIVE UP WAITING FOR ANOTHER THREAD TO SIGNAL :return: """ if self.waiting: waiter = self.waiting.pop() waiter.go() try: waiter = Signal() self.waiting.appendleft(waiter) self.lock.release() (waiter | till).wait_for_go() return not not waiter finally: self.lock.acquire()
from __future__ import absolute_import from __future__ import division from __future__ import unicode_literals from thread import allocate_lock as _allocate_lock from time import sleep, time from pyLibrary.thread.signal import Signal from pyLibrary.times.dates import Date from pyLibrary.times.durations import Duration INTERVAL = 0.1 _till_locker = _allocate_lock() next_ping = time() done = Signal("Timers shutdown") done.go() class Till(Signal): """ TIMEOUT AS A SIGNAL """ enabled = False new_timers = [] def __new__(cls, till=None, timeout=None, seconds=None): if not Till.enabled: return done elif till is None and timeout is None and seconds is None: return None
from __future__ import absolute_import from __future__ import division from __future__ import unicode_literals from thread import allocate_lock as _allocate_lock from time import sleep as _sleep from time import time as _time from pyLibrary.thread.signal import Signal from pyLibrary.times.dates import Date from pyLibrary.times.durations import Duration DEBUG = True INTERVAL = 0.1 next_ping = _time() done = Signal("Timers shutdown") done.go() class Till(Signal): """ TIMEOUT AS A SIGNAL """ enabled = False all_timers = [] locker = _allocate_lock() def __new__(cls, till=None, timeout=None, seconds=None): if not Till.enabled: return done