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
python类_get_ident()的实例源码
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]
trace.py 文件源码
项目:Comparative-Annotation-Toolkit
作者: ComparativeGenomicsToolkit
项目源码
文件源码
阅读 31
收藏 0
点赞 0
评论 0
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
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
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
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)
def _get_threading_ident():
if sys.version_info >= (3, 3):
return threading.get_ident()
return threading._get_ident()
def _thread_ident():
return -1
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]
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()))
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
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
def thread_protocol(self):
"""
Protocol instance corresponding to the current thread. Not
intended to be called from connection thread.
"""
return self.protocols[get_ident()]
def _get_threading_ident():
if sys.version_info >= (3, 3):
return threading.get_ident()
return threading._get_ident()
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)
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