Python threading._get_ident() Examples
The following are 19
code examples of threading._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
threading
, or try the search function
.
Example #1
Source File: helpers.py From panoptes with Apache License 2.0 | 6 votes |
def get_os_tid(): """ Get the Linux process id associated with the current thread Returns: int: The process id """ if sys.platform.startswith(u'linux'): return ctypes.CDLL(u'libc.so.6').syscall(186) else: # TODO: This is hacky - we need to replace it with something that actually returns the OS thread ID if is_python_2(): return threading._get_ident() else: return threading.get_ident()
Example #2
Source File: trace.py From Comparative-Annotation-Toolkit with Apache License 2.0 | 6 votes |
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 #3
Source File: cpstats.py From opsbro with MIT License | 6 votes |
def record_start(self): """Record the beginning of a request.""" request = cherrypy.serving.request if not hasattr(request.rfile, 'bytes_read'): request.rfile = ByteCountWrapper(request.rfile) request.body.fp = request.rfile r = request.remote appstats['Current Requests'] += 1 appstats['Total Requests'] += 1 appstats['Requests'][threading._get_ident()] = { 'Bytes Read': None, 'Bytes Written': None, # Use a lambda so the ip gets updated by tools.proxy later 'Client': lambda s: '%s:%s' % (r.ip, r.port), 'End Time': None, 'Processing Time': proc_time, 'Request-Line': request.request_line, 'Response Status': None, 'Start Time': time.time(), }
Example #4
Source File: cpstats.py From moviegrabber with GNU General Public License v3.0 | 6 votes |
def record_start(self): """Record the beginning of a request.""" request = cherrypy.serving.request if not hasattr(request.rfile, 'bytes_read'): request.rfile = ByteCountWrapper(request.rfile) request.body.fp = request.rfile r = request.remote appstats['Current Requests'] += 1 appstats['Total Requests'] += 1 appstats['Requests'][threading._get_ident()] = { 'Bytes Read': None, 'Bytes Written': None, # Use a lambda so the ip gets updated by tools.proxy later 'Client': lambda s: '%s:%s' % (r.ip, r.port), 'End Time': None, 'Processing Time': proc_time, 'Request-Line': request.request_line, 'Response Status': None, 'Start Time': time.time(), }
Example #5
Source File: __init__.py From trains with Apache License 2.0 | 6 votes |
def _patched_call(original_fn, patched_fn): def _inner_patch(*args, **kwargs): # noinspection PyProtectedMember,PyUnresolvedReferences ident = threading._get_ident() if six.PY2 else threading.get_ident() if ident in _recursion_guard: return original_fn(*args, **kwargs) _recursion_guard[ident] = 1 ret = None try: ret = patched_fn(original_fn, *args, **kwargs) except Exception as ex: raise ex finally: try: _recursion_guard.pop(ident) except KeyError: pass return ret return _inner_patch
Example #6
Source File: matplotlib_bind.py From trains with Apache License 2.0 | 6 votes |
def patched_show(*args, **kw): tid = threading._get_ident() if six.PY2 else threading.get_ident() PatchedMatplotlib._recursion_guard[tid] = True # noinspection PyBroadException try: figures = PatchedMatplotlib._get_output_figures(None, all_figures=True) for figure in figures: # if this is a stale figure (just updated) we should send it, the rest will not be stale if figure.canvas.figure.stale or (hasattr(figure, '_trains_is_imshow') and figure._trains_is_imshow): PatchedMatplotlib._report_figure(stored_figure=figure) except Exception: pass ret = PatchedMatplotlib._patched_original_plot(*args, **kw) if PatchedMatplotlib._current_task and sys.modules['matplotlib'].rcParams['backend'] == 'agg': # clear the current plot, because no one else will # noinspection PyBroadException try: if sys.modules['matplotlib'].rcParams['backend'] == 'agg': import matplotlib.pyplot as plt plt.clf() except Exception: pass PatchedMatplotlib._recursion_guard[tid] = False return ret
Example #7
Source File: chainmap.py From python-compat-runtime with Apache License 2.0 | 6 votes |
def _recursive_repr(fillvalue='...'): 'Decorator to make a repr function return fillvalue for a recursive call' def decorating_function(user_function): repr_running = set() def wrapper(self): key = id(self), get_ident() if key in repr_running: return fillvalue repr_running.add(key) try: result = user_function(self) finally: repr_running.discard(key) return result # Can't use functools.wraps() here because of bootstrap issues wrapper.__module__ = getattr(user_function, '__module__') wrapper.__doc__ = getattr(user_function, '__doc__') wrapper.__name__ = getattr(user_function, '__name__') wrapper.__annotations__ = getattr(user_function, '__annotations__', {}) return wrapper return decorating_function
Example #8
Source File: threading.py From PhonePi_SampleServer with MIT License | 6 votes |
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 #9
Source File: threading.py From PokemonGo-DesktopMap with MIT License | 5 votes |
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 #10
Source File: cpstats.py From cherrypy with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _get_threading_ident(): if sys.version_info >= (3, 3): return threading.get_ident() return threading._get_ident()
Example #11
Source File: threading.py From PokemonGo-DesktopMap with MIT License | 5 votes |
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 #12
Source File: cpstats.py From Tautulli with GNU General Public License v3.0 | 5 votes |
def _get_threading_ident(): if sys.version_info >= (3, 3): return threading.get_ident() return threading._get_ident()
Example #13
Source File: cpstats.py From bazarr with GNU General Public License v3.0 | 5 votes |
def _get_threading_ident(): if sys.version_info >= (3, 3): return threading.get_ident() return threading._get_ident()
Example #14
Source File: matplotlib_bind.py From trains with Apache License 2.0 | 5 votes |
def patched_figure_show(self, *args, **kw): tid = threading._get_ident() if six.PY2 else threading.get_ident() if PatchedMatplotlib._recursion_guard.get(tid): # we are inside a gaurd do nothing return PatchedMatplotlib._patched_original_figure(self, *args, **kw) PatchedMatplotlib._recursion_guard[tid] = True PatchedMatplotlib._report_figure(set_active=False, specific_fig=self) ret = PatchedMatplotlib._patched_original_figure(self, *args, **kw) PatchedMatplotlib._recursion_guard[tid] = False return ret
Example #15
Source File: matplotlib_bind.py From trains with Apache License 2.0 | 5 votes |
def patched_savefig(self, *args, **kw): ret = PatchedMatplotlib._patched_original_savefig(self, *args, **kw) # noinspection PyBroadException try: fname = kw.get('fname') or args[0] from pathlib2 import Path if six.PY3: from pathlib import Path as Path3 else: Path3 = Path # if we are not storing into a file (str/Path) do not log the matplotlib if not isinstance(fname, (str, Path, Path3)): return ret except Exception: pass tid = threading._get_ident() if six.PY2 else threading.get_ident() if not PatchedMatplotlib._recursion_guard.get(tid): PatchedMatplotlib._recursion_guard[tid] = True # noinspection PyBroadException try: PatchedMatplotlib._report_figure(specific_fig=self, set_active=False) except Exception: pass PatchedMatplotlib._recursion_guard[tid] = False return ret
Example #16
Source File: threading.py From satori with Apache License 2.0 | 5 votes |
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 #17
Source File: cpstats.py From opsbro with MIT License | 4 votes |
def record_stop( self, uriset=None, slow_queries=1.0, slow_queries_count=100, debug=False, **kwargs): """Record the end of a request.""" resp = cherrypy.serving.response w = appstats['Requests'][threading._get_ident()] r = cherrypy.request.rfile.bytes_read w['Bytes Read'] = r appstats['Total Bytes Read'] += r if resp.stream: w['Bytes Written'] = 'chunked' else: cl = int(resp.headers.get('Content-Length', 0)) w['Bytes Written'] = cl appstats['Total Bytes Written'] += cl w['Response Status'] = getattr( resp, 'output_status', None) or resp.status w['End Time'] = time.time() p = w['End Time'] - w['Start Time'] w['Processing Time'] = p appstats['Total Time'] += p appstats['Current Requests'] -= 1 if debug: cherrypy.log('Stats recorded: %s' % repr(w), 'TOOLS.CPSTATS') if uriset: rs = appstats.setdefault('URI Set Tracking', {}) r = rs.setdefault(uriset, { 'Min': None, 'Max': None, 'Count': 0, 'Sum': 0, 'Avg': average_uriset_time}) if r['Min'] is None or p < r['Min']: r['Min'] = p if r['Max'] is None or p > r['Max']: r['Max'] = p r['Count'] += 1 r['Sum'] += p if slow_queries and p > slow_queries: sq = appstats.setdefault('Slow Queries', []) sq.append(w.copy()) if len(sq) > slow_queries_count: sq.pop(0)
Example #18
Source File: cpstats.py From moviegrabber with GNU General Public License v3.0 | 4 votes |
def record_stop(self, uriset=None, slow_queries=1.0, slow_queries_count=100, debug=False, **kwargs): """Record the end of a request.""" resp = cherrypy.serving.response w = appstats['Requests'][threading._get_ident()] r = cherrypy.request.rfile.bytes_read w['Bytes Read'] = r appstats['Total Bytes Read'] += r if resp.stream: w['Bytes Written'] = 'chunked' else: cl = int(resp.headers.get('Content-Length', 0)) w['Bytes Written'] = cl appstats['Total Bytes Written'] += cl w['Response Status'] = getattr(resp, 'output_status', None) or resp.status w['End Time'] = time.time() p = w['End Time'] - w['Start Time'] w['Processing Time'] = p appstats['Total Time'] += p appstats['Current Requests'] -= 1 if debug: cherrypy.log('Stats recorded: %s' % repr(w), 'TOOLS.CPSTATS') if uriset: rs = appstats.setdefault('URI Set Tracking', {}) r = rs.setdefault(uriset, { 'Min': None, 'Max': None, 'Count': 0, 'Sum': 0, 'Avg': average_uriset_time}) if r['Min'] is None or p < r['Min']: r['Min'] = p if r['Max'] is None or p > r['Max']: r['Max'] = p r['Count'] += 1 r['Sum'] += p if slow_queries and p > slow_queries: sq = appstats.setdefault('Slow Queries', []) sq.append(w.copy()) if len(sq) > slow_queries_count: sq.pop(0)
Example #19
Source File: attach_script.py From PyDev.Debugger with Eclipse Public License 1.0 | 4 votes |
def fix_main_thread_id(on_warn=lambda msg:None, on_exception=lambda msg:None, on_critical=lambda msg:None): # This means that we weren't able to import threading in the main thread (which most # likely means that the main thread is paused or in some very long operation). # In this case we'll import threading here and hotfix what may be wrong in the threading # module (if we're on Windows where we create a thread to do the attach and on Linux # we are not certain on which thread we're executing this code). # # The code below is a workaround for https://bugs.python.org/issue37416 import sys import threading try: with threading._active_limbo_lock: main_thread_instance = get_main_thread_instance(threading) if sys.platform == 'win32': # On windows this code would be called in a secondary thread, so, # the current thread is unlikely to be the main thread. if hasattr(threading, '_get_ident'): unlikely_thread_id = threading._get_ident() # py2 else: unlikely_thread_id = threading.get_ident() # py3 else: unlikely_thread_id = None main_thread_id, critical_warning = get_main_thread_id(unlikely_thread_id) if main_thread_id is not None: main_thread_id_attr = '_ident' if not hasattr(main_thread_instance, main_thread_id_attr): main_thread_id_attr = '_Thread__ident' assert hasattr(main_thread_instance, main_thread_id_attr) if main_thread_id != getattr(main_thread_instance, main_thread_id_attr): # Note that we also have to reset the '_tstack_lock' for a regular lock. # This is needed to avoid an error on shutdown because this lock is bound # to the thread state and will be released when the secondary thread # that initialized the lock is finished -- making an assert fail during # process shutdown. main_thread_instance._tstate_lock = threading._allocate_lock() main_thread_instance._tstate_lock.acquire() # Actually patch the thread ident as well as the threading._active dict # (we should have the _active_limbo_lock to do that). threading._active.pop(getattr(main_thread_instance, main_thread_id_attr), None) setattr(main_thread_instance, main_thread_id_attr, main_thread_id) threading._active[getattr(main_thread_instance, main_thread_id_attr)] = main_thread_instance # Note: only import from pydevd after the patching is done (we want to do the minimum # possible when doing that patching). on_warn('The threading module was not imported by user code in the main thread. The debugger will attempt to work around https://bugs.python.org/issue37416.') if critical_warning: on_critical('Issue found when debugger was trying to work around https://bugs.python.org/issue37416:\n%s' % (critical_warning,)) except: on_exception('Error patching main thread id.')