示例#1
0
 def __init__(self, timeout=None, verbose=None):
     threading._Verbose.__init__(self, verbose)
     self.__block = _allocate_lock()
     self.__owner = None
     self.__count = 0
     self.__timeout = timeout
     self.__acquiredStackTrace = None
示例#2
0
 def __init__(self, timeout=None, verbose=None):
     threading._Verbose.__init__(self, verbose)
     self.__block = _allocate_lock()
     self.__owner = None
     self.__count = 0
     self.__timeout = timeout
     self.__acquiredStackTrace = None
示例#3
0
 def __init__(self, name=None):
     if DEBUG:
         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
示例#4
0
 def __init__(self, name=None):
     if DEBUG:
         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
示例#5
0
 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
示例#6
0
 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
示例#7
0
    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()
示例#8
0
    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()
示例#9
0
class Till(Signal):
    """
    TIMEOUT AS A SIGNAL
    """
    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 is None and timeout is None and seconds is 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

        Signal.__init__(self, name=unicode(timeout))

        with Till.locker:
            if timeout != None:
                Till.next_ping = min(Till.next_ping, timeout)
            Till.new_timers.append((timeout, self))
示例#10
0
    def wait_for_go(self):
        """
        PUT THREAD IN WAIT STATE UNTIL SIGNAL IS ACTIVATED
        """
        with self.lock:
            if self._go:
                return True
            stopper = _allocate_lock()
            stopper.acquire()
            self.waiting_threads.append(stopper)

        if DEBUG:
            if not _Log:
                _late_import()
            _Log.note("wait for go {{name|quote}}", name=self.name)
        stopper.acquire()
        if DEBUG:
            if not _Log:
                _late_import()
            _Log.note("GOing! {{name|quote}}", name=self.name)
        return True
示例#11
0
    def wait_for_go(self):
        """
        PUT THREAD IN WAIT STATE UNTIL SIGNAL IS ACTIVATED
        """
        with self.lock:
            if self._go:
                return True
            stopper = _allocate_lock()
            stopper.acquire()
            self.waiting_threads.append(stopper)

        if DEBUG:
            if not _Log:
                _late_import()
            _Log.note("wait for go {{name|quote}}", name=self.name)
        stopper.acquire()
        if DEBUG:
            if not _Log:
                _late_import()
            _Log.note("GOing! {{name|quote}}", name=self.name)
        return True
示例#12
0
    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)

        if DEBUG:
            Log.note("wait for go {{name|quote}}", name=self.name)
        stopper.acquire()
        if DEBUG:
            Log.note("GOing! {{name|quote}}", name=self.name)
        return True
示例#13
0
    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)

        if DEBUG:
            Log.note("wait for go {{name|quote}}", name=self.name)
        stopper.acquire()
        if DEBUG:
            Log.note("GOing! {{name|quote}}", name=self.name)
        return True
示例#14
0
 def __init__(self):
     self._owner = None
     self._block = _allocate_lock()
     self._locking = {}
     self._count = 0
示例#15
0
    'BoundedSemaphore',
    'RLock',
]

# On PyPy, we don't compile the Semaphore class with Cython. Under
# Cython, each individual method holds the GIL for its entire
# duration, ensuring that no other thread can interrupt us in an
# unsafe state (only when we _do_wait do we call back into Python and
# allow switching threads). Simulate that here through the use of a manual
# lock. (We use a separate lock for each semaphore to allow sys.settrace functions
# to use locks *other* than the one being traced.)
if PYPY:
    # TODO: Need to use monkey.get_original?
    from thread import allocate_lock as _allocate_lock # pylint:disable=import-error,useless-suppression
    from thread import get_ident as _get_ident # pylint:disable=import-error,useless-suppression
    _sem_lock = _allocate_lock()

    def untraceable(f):
        # Don't allow re-entry to these functions in a single thread, as can
        # happen if a sys.settrace is used
        def wrapper(self):
            me = _get_ident()
            try:
                count = self._locking[me]
            except KeyError:
                count = self._locking[me] = 1
            else:
                count = self._locking[me] = count + 1
            if count:
                return
示例#16
0
 def __init__(self, name=""):
     self.name = name
     self.lock = _allocate_lock()
     self.waiting = deque()
示例#17
0
# THIS SIGNAL IS IMPORTANT FOR PROPER SIGNALLING WHICH ALLOWS
# FOR FAST AND PREDICTABLE SHUTDOWN AND CLEANUP OF THREADS

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 mo_threads.signal import Signal

DEBUG = False
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
示例#18
0
# FOR FAST AND PREDICTABLE SHUTDOWN AND CLEANUP OF THREADS

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
示例#19
0
 def __init__(self, signal):
     self.signal = signal
     self.locker = _allocate_lock()
     self.inc = 0
示例#20
0
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
        elif till is None and timeout is None and seconds is None:
            return None
        else:
            return object.__new__(cls)

    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))

    @classmethod
    def daemon(cls, please_stop):
        global next_ping

        Till.enabled = True
        try:
            while not please_stop:
                now = _time()
                with Till.locker:
                    if next_ping > now:
                        _sleep(min(next_ping - now, INTERVAL))
                        continue

                    next_ping = now + INTERVAL
                    work = None
                    if Till.all_timers:
                        Till.all_timers.sort(key=lambda r: r[0])
                        for i, (t, s) in enumerate(Till.all_timers):
                            if now < t:
                                work, Till.all_timers[:i] = Till.all_timers[:i], []
                                next_ping = min(next_ping, Till.all_timers[0][0])
                                break
                        else:
                            work, Till.all_timers = Till.all_timers, []

                if work:
                    for t, s in work:
                        s.go()

        except Exception, e:
            from pyLibrary.debugs.logs import Log

            Log.warning("timer shutdown", cause=e)
        finally:
示例#21
0
 def __init__(self, name=""):
     if DEBUG and not _Log:
         _late_import()
     self.name = name
     self.lock = _allocate_lock()
     self.waiting = None
示例#22
0
    return datetime.utcfromtimestamp(unix)


def unix2Date(unix):
    if not isinstance(unix, float):
        from pyLibrary.debugs.logs import Log
        Log.error("problem")

    output = object.__new__(Date)
    output.unix = unix
    return output


LEAP_SECOND = Date("1jan2017").unix
_leap_log = []
_leap_lock = _allocate_lock()
_Log = None


def _leap_logging(clock_time, utc_time):
    global _Log
    global _leap_log

    diff = clock_time - LEAP_SECOND
    with _leap_lock:
        if not _Log:
            from pyLibrary.debugs.logs import Log as _Log
            _Log.warning("preparing for leap seconds near {{date|datetime}}", date=LEAP_SECOND)

    if abs(diff) < 30:  # RECORD AROUND THE LEAP SECOND
        with _leap_lock:
示例#23
0
 def __init__(self, signal):
     self.signal = signal
     self.locker = _allocate_lock()
     self.inc = 0