Python six.moves._thread.get_ident() Examples

The following are 11 code examples of six.moves._thread.get_ident(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module six.moves._thread , or try the search function .
Example #1
Source File: Worker.py    From OpenPLC_Editor with GNU General Public License v3.0 6 votes vote down vote up
def runloop(self, *args, **kwargs):
        """
        meant to be called by worker thread (blocking)
        """
        self._threadID = _thread.get_ident()
        self.mutex.acquire()
        if args or kwargs:
            _job = job(*args, **kwargs)
            _job.do()
            if not _job.success:
                self.reraise(_job)

        while not self._finish:
            self.todo.wait()
            if self.job is not None:
                self.job.do()
                self.done.notify()
            else:
                break

        self.mutex.release() 
Example #2
Source File: samplers.py    From profiling with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _profile(self, profiler, frame, event, arg):
        t = thread_clock()
        thread_id = _thread.get_ident()
        sampled_at = self.sampled_times.get(thread_id, 0)
        if t - sampled_at < self.interval:
            return
        self.sampled_times[thread_id] = t
        profiler.sample(frame)
        self.counter += 1
        if self.counter % 10000 == 0:
            self._clear_for_dead_threads() 
Example #3
Source File: data.py    From zipline-chinese with Apache License 2.0 5 votes vote down vote up
def __repr__(self, _repr_running={}):
        # Based on OrderedDict/defaultdict
        call_key = id(self), get_ident()
        if call_key in _repr_running:
            return '...'
        _repr_running[call_key] = 1
        try:
            if not self:
                return '%s(%r)' % (self.__class__.__name__, self._sort_key)
            return '%s(%r, %r)' % (self.__class__.__name__, self._sort_key,
                                   list(self.items()))
        finally:
            del _repr_running[call_key] 
Example #4
Source File: util.py    From virt-who with GNU General Public License v2.0 5 votes vote down vote up
def __repr__(self, _repr_running={}):
        'od.__repr__() <==> repr(od)'
        call_key = id(self), _get_ident()
        if call_key in _repr_running:
            return '...'
        _repr_running[call_key] = 1
        try:
            if not self:
                return '%s()' % (self.__class__.__name__,)
            return '%s(%r)' % (self.__class__.__name__, list(self.items()))
        finally:
            del _repr_running[call_key] 
Example #5
Source File: threading_utils.py    From taskflow with Apache License 2.0 5 votes vote down vote up
def get_ident():
    """Return the 'thread identifier' of the current thread."""
    return _thread.get_ident() 
Example #6
Source File: local.py    From treadmill with Apache License 2.0 5 votes vote down vote up
def _temp_dir():
    """Construct an empty temporary dir for each thread and return the path."""
    dirname = os.path.join(tempfile.gettempdir(),
                           'local-{}.temp'.format(_thread.get_ident()))

    shutil.rmtree(dirname, True)
    os.mkdir(dirname, 0o755)
    return dirname 
Example #7
Source File: local_test.py    From treadmill with Apache License 2.0 5 votes vote down vote up
def test_concat_files(self):
        """Test the _concat_files() func."""
        ident = _thread.get_ident()
        file_lst = []
        for i in six.moves.range(3):
            file_lst.append(os.path.join(tempfile.gettempdir(),
                                         '{}.{}'.format(ident, i)))
            with io.open(file_lst[-1], 'wb') as logs:
                logs.write(bytearray('{}\n'.format(i), 'ascii'))

        result = local._concat_files(file_lst)
        self.assertTrue(isinstance(result, io.TextIOWrapper))
        self.assertEqual(result.read(), u'0\n1\n2\n')

        # check that _concat_files() catches IOError for non existing file
        file_lst.append('no_such_file')
        local._concat_files(file_lst)

        for f in file_lst[:-1]:
            os.remove(f)

        # make sure that things don't break if the log file contains some
        # binary data with ord num > 128 (eg. \xc5 below) ie. not ascii
        # decodeable
        with tempfile.NamedTemporaryFile(mode='wb', delete=False) as temp:
            temp.write(b'\x42\x00\x01\xc5\x45\x0a')
            temp.seek(0)

        self.assertTrue(''.join(local._concat_files([temp.name]))) 
Example #8
Source File: plugins.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def acquire_thread(self):
        """Run 'start_thread' listeners for the current thread.

        If the current thread has already been seen, any 'start_thread'
        listeners will not be run again.
        """
        thread_ident = _thread.get_ident()
        if thread_ident not in self.threads:
            # We can't just use get_ident as the thread ID
            # because some platforms reuse thread ID's.
            i = len(self.threads) + 1
            self.threads[thread_ident] = i
            self.bus.publish('start_thread', i) 
Example #9
Source File: plugins.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def release_thread(self):
        """Release the current thread and run 'stop_thread' listeners."""
        thread_ident = _thread.get_ident()
        i = self.threads.pop(thread_ident, None)
        if i is not None:
            self.bus.publish('stop_thread', i) 
Example #10
Source File: Worker.py    From OpenPLC_Editor with GNU General Public License v3.0 5 votes vote down vote up
def call(self, *args, **kwargs):
        """
        creates a job, execute it in worker thread, and deliver result.
        if job execution raise exception, re-raise same exception
        meant to be called by non-worker threads, but this is accepted.
        blocking until job done
        """

        _job = job(*args, **kwargs)

        if self._threadID == _thread.get_ident():
            # if caller is worker thread execute immediately
            _job.do()
        else:
            # otherwise notify and wait for completion
            self.mutex.acquire()

            while self.job is not None:
                self.free.wait()

            self.job = _job
            self.todo.notify()
            self.done.wait()
            self.job = None
            self.free.notify()
            self.mutex.release()

        if _job.success:
            return _job.result
        else:
            self.reraise(_job) 
Example #11
Source File: threads.py    From openhtf with Apache License 2.0 4 votes vote down vote up
def _safe_lock_release_py2(rlock):
  """Ensure that a threading.RLock is fully released for Python 2.

  The RLock release code is:
    https://github.com/python/cpython/blob/2.7/Lib/threading.py#L187

  The RLock object's release method does not release all of its state if an
  exception is raised in the middle of its operation.  There are three pieces of
  internal state that must be cleaned up:
  - owning thread ident, an integer.
  - entry count, an integer that counts how many times the current owner has
      locked the RLock.
  - internal lock, a threading.Lock instance that handles blocking.

  Args:
    rlock: threading.RLock, lock to fully release.

  Yields:
    None.
  """
  assert isinstance(rlock, threading._RLock)
  ident = _thread.get_ident()
  expected_count = 0
  if rlock._RLock__owner == ident:
    expected_count = rlock._RLock__count
  try:
    yield
  except ThreadTerminationError:
    # Check if the current thread still owns the lock by checking if we can
    # acquire the underlying lock.
    if rlock._RLock__block.acquire(0):
      # Lock is clean, so unlock and we are done.
      rlock._RLock__block.release()
    elif rlock._RLock__owner == ident and expected_count > 0:
      # The lock is still held up the stack, so make sure the count is accurate.
      if rlock._RLock__count != expected_count:
        rlock._RLock__count = expected_count
    elif rlock._RLock__owner == ident or rlock._RLock__owner is None:
      # The internal lock is still acquired, but either this thread or no thread
      # owns it, which means it needs to be hard reset.
      rlock._RLock__owner = None
      rlock._RLock__count = 0
      rlock._RLock__block.release()
    raise
# pylint: enable=protected-access