Python threading._get_ident() Examples

The following are 16 code examples for showing how to use threading._get_ident(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don't like. You can also save this page to your account.

Example 1
Project: RealtimePythonChat   Author: quangtqag   File: threading.py    (license) View Source Project 6 votes vote down vote up
def __init__(self):
        #_DummyThread_.__init__(self) # pylint:disable=super-init-not-called

        # It'd be nice to use a pattern like "greenlet-%d", but maybe somebody out
        # there is checking thread names...
        self._name = self._Thread__name = __threading__._newname("DummyThread-%d")
        self._set_ident()

        g = getcurrent()
        gid = _get_ident(g) # same as id(g)
        __threading__._active[gid] = self
        rawlink = getattr(g, 'rawlink', None)
        if rawlink is not None:
            # raw greenlet.greenlet greenlets don't
            # have rawlink...
            rawlink(_cleanup)
        else:
            # ... so for them we use weakrefs.
            # See https://github.com/gevent/gevent/issues/918
            global _weakref
            if _weakref is None:
                _weakref = __import__('weakref')
            ref = _weakref.ref(g, _make_cleanup_id(gid))
            self.__raw_ref = ref 
Example 2
Project: midict   Author: ShenggaoZhu   File: __init__.py    (license) View Source Project 6 votes vote down vote up
def __repr__(self, _repr_running={}):
        'repr as "MIDict(items, names)"'
        call_key = id(self), _get_ident()
        if call_key in _repr_running: # pragma: no cover
            return '<%s(...)>' % self.__class__.__name__
        _repr_running[call_key] = 1
        try:
            try:
                if self.indices:
                    names = force_list(self.indices.keys())
                    items = force_list(self.items())
                    return '%s(%s, %s)' % (self.__class__.__name__, items, names)
            except AttributeError: # pragma: no cover
                # may not have attr ``indices`` yet
                pass
            return '%s()' % self.__class__.__name__
        finally:
            del _repr_running[call_key] 
Example 3
Project: Comparative-Annotation-Toolkit   Author: ComparativeGenomicsToolkit   File: trace.py    (license) View Source Project 6 votes vote down vote up
def log(self, *args):
        """log arguments as a message followed by a newline"""
        # build string and output, as this minimize interleaved messages
        # discard output on I/O errors
        try:
            msg = []
            if self.inclPid:
                msg.append(str(os.getpid()))
                msg.append(": ")
            if self.inclThread:
                # can only include id, getting name will cause deadlock
                msg.append(str(threading._get_ident()))
                msg.append(": ")
            for a in args:
                msg.append(str(a))
            msg.append("\n")
            self.fh.write("".join(msg))
            self.fh.flush()
        except IOError as ex:
            pass 
Example 4
Project: asgi_rabbitmq   Author: proofit404   File: core.py    (license) View Source Project 6 votes vote down vote up
def schedule(self, f, *args, **kwargs):
        """
        Try to acquire connection access lock.  Then call protocol method.
        Return concurrent Future instance you can wait in the other
        thread.
        """

        self.wait_open()
        # RabbitMQ operations are multiplexed between different AMQP
        # method callbacks.  Final result of the protocol method call
        # will be set inside one of this callbacks.  So other thread
        # will be able to wait unless this event happens in the
        # connection event loop.
        future = Future()
        with self.lock:
            self.process(get_ident(), (f, args, kwargs), future)
        return future 
Example 5
Project: zenchmarks   Author: squeaky-pl   File: threading.py    (license) View Source Project 6 votes vote down vote up
def __init__(self):
        #_DummyThread_.__init__(self) # pylint:disable=super-init-not-called

        # It'd be nice to use a pattern like "greenlet-%d", but maybe somebody out
        # there is checking thread names...
        self._name = self._Thread__name = __threading__._newname("DummyThread-%d")
        self._set_ident()

        g = getcurrent()
        gid = _get_ident(g) # same as id(g)
        __threading__._active[gid] = self
        rawlink = getattr(g, 'rawlink', None)
        if rawlink is not None:
            # raw greenlet.greenlet greenlets don't
            # have rawlink...
            rawlink(_cleanup)
        else:
            # ... so for them we use weakrefs.
            # See https://github.com/gevent/gevent/issues/918
            global _weakref
            if _weakref is None:
                _weakref = __import__('weakref')
            ref = _weakref.ref(g, _make_cleanup_id(gid))
            self.__raw_ref = ref 
Example 6
Project: oa_qian   Author: sunqb   File: threading.py    (license) View Source Project 5 votes vote down vote up
def __init__(self):
        #_DummyThread_.__init__(self)

        # It'd be nice to use a pattern like "greenlet-%d", but maybe somebody out
        # there is checking thread names...
        self._name = self._Thread__name = __threading__._newname("DummyThread-%d")
        self._set_ident()

        __threading__._active[_get_ident()] = self
        g = getcurrent()
        rawlink = getattr(g, 'rawlink', None)
        if rawlink is not None:
            rawlink(_cleanup) 
Example 7
Project: autosub-bootstrapbill   Author: BenjV   File: cpstats.py    (license) View Source Project 5 votes vote down vote up
def _get_threading_ident():
    if sys.version_info >= (3, 3):
        return threading.get_ident()
    return threading._get_ident() 
Example 8
Project: pefile.pypy   Author: cloudtracer   File: _collections.py    (license) View Source Project 5 votes vote down vote up
def _thread_ident():
        return -1 
Example 9
Project: pefile.pypy   Author: cloudtracer   File: _collections.py    (license) View Source Project 5 votes vote down vote up
def __repr__(self):
        threadlocalattr = '__repr' + str(_thread_ident())
        if threadlocalattr in self.__dict__:
            return 'deque([...])'
        else:
            self.__dict__[threadlocalattr] = True
            try:
                if self.maxlen is not None:
                    return 'deque(%r, maxlen=%s)' % (list(self), self.maxlen)
                else:
                    return 'deque(%r)' % (list(self),)
            finally:
                del self.__dict__[threadlocalattr] 
Example 10
Project: pefile.pypy   Author: cloudtracer   File: _sqlite3.py    (license) View Source Project 5 votes vote down vote up
def _check_thread(self):
        try:
            if self.__thread_ident == _thread_get_ident():
                return
        except AttributeError:
            pass
        else:
            raise ProgrammingError(
                "SQLite objects created in a thread can only be used in that "
                "same thread. The object was created in thread id %d and this "
                "is thread id %d" % (self.__thread_ident, _thread_get_ident())) 
Example 11
Project: bzmzq   Author: Blazemeter   File: rlock.py    (license) View Source Project 5 votes vote down vote up
def acquire(self, blocking=True, timeout=None):
        with self._reference_lock:
            # You are the man, bump the ref count
            if self._lock_holding_thread == _get_ident():
                self._reference_count += 1
                return True

        lock_result = super(RLock, self).acquire(blocking, timeout)

        if lock_result:
            with self._reference_lock:
                self._lock_holding_thread = _get_ident()
                self._reference_count = 1
        return lock_result 
Example 12
Project: bzmzq   Author: Blazemeter   File: rlock.py    (license) View Source Project 5 votes vote down vote up
def release(self):
        with self._reference_lock:
            # You never had the lock :(
            if self._lock_holding_thread != _get_ident():
                return False

            self._reference_count -= 1
            if self._reference_count == 0:
                self.__rlock_cleanup()
                return self.client.retry(self._inner_release)
            return True 
Example 13
Project: asgi_rabbitmq   Author: proofit404   File: core.py    (license) View Source Project 5 votes vote down vote up
def thread_protocol(self):
        """
        Protocol instance corresponding to the current thread.  Not
        intended to be called from connection thread.
        """

        return self.protocols[get_ident()] 
Example 14
Project: watcher   Author: nosmokingbandit   File: cpstats.py    (license) View Source Project 5 votes vote down vote up
def _get_threading_ident():
    if sys.version_info >= (3, 3):
        return threading.get_ident()
    return threading._get_ident() 
Example 15
Project: Lixiang_zhaoxin   Author: hejaxian   File: threading.py    (license) View Source Project 5 votes vote down vote up
def __init__(self):
        #_DummyThread_.__init__(self)

        # It'd be nice to use a pattern like "greenlet-%d", but maybe somebody out
        # there is checking thread names...
        self._name = self._Thread__name = __threading__._newname("DummyThread-%d")
        self._set_ident()

        __threading__._active[_get_ident()] = self
        g = getcurrent()
        rawlink = getattr(g, 'rawlink', None)
        if rawlink is not None:
            rawlink(_cleanup) 
Example 16
Project: pefile.pypy   Author: cloudtracer   File: _sqlite3.py    (license) View Source Project 4 votes vote down vote up
def __init__(self, database, timeout=5.0, detect_types=0, isolation_level="",
                 check_same_thread=True, factory=None, cached_statements=100):
        self.__initialized = True
        db_star = _ffi.new('sqlite3 **')

        if isinstance(database, unicode):
            database = database.encode('utf-8')
        if _lib.sqlite3_open(database, db_star) != _lib.SQLITE_OK:
            raise OperationalError("Could not open database")
        self._db = db_star[0]
        if timeout is not None:
            timeout = int(timeout * 1000)  # pysqlite2 uses timeout in seconds
            _lib.sqlite3_busy_timeout(self._db, timeout)

        self.row_factory = None
        self.text_factory = _unicode_text_factory

        self._detect_types = detect_types
        self._in_transaction = False
        self.isolation_level = isolation_level

        self.__cursors = []
        self.__cursors_counter = 0
        self.__statements = []
        self.__statements_counter = 0
        self.__rawstatements = set()
        self._statement_cache = _StatementCache(self, cached_statements)

        self.__func_cache = {}
        self.__aggregates = {}
        self.__aggregate_instances = {}
        self.__collations = {}
        if check_same_thread:
            self.__thread_ident = _thread_get_ident()

        self.Error = Error
        self.Warning = Warning
        self.InterfaceError = InterfaceError
        self.DatabaseError = DatabaseError
        self.InternalError = InternalError
        self.OperationalError = OperationalError
        self.ProgrammingError = ProgrammingError
        self.IntegrityError = IntegrityError
        self.DataError = DataError
        self.NotSupportedError = NotSupportedError