Python threading.setprofile() Examples
The following are 15
code examples of threading.setprofile().
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: __init__.py From profiling with BSD 3-Clause "New" or "Revised" License | 6 votes |
def run(self): if sys.getprofile() is not None: # NOTE: There's no threading.getprofile(). # The profiling function will be stored at threading._profile_hook # but it's not documented. raise RuntimeError('Another profiler already registered') with deferral() as defer: self._times_entered.clear() self.overhead = 0.0 sys.setprofile(self._profile) defer(sys.setprofile, None) threading.setprofile(self._profile) defer(threading.setprofile, None) self.timer.start(self) defer(self.timer.stop) yield
Example #2
Source File: type_util.py From pytypes with Apache License 2.0 | 6 votes |
def restore_profiler(): """If a typechecking profiler is active, e.g. created by pytypes.set_global_typechecked_profiler(), such a profiler must be restored whenever a TypeCheckError is caught. The call must stem from the thread that raised the error. Otherwise the typechecking profiler is implicitly disabled. Alternatively one can turn pytypes into warning mode. In that mode no calls to this function are required (unless one uses filterwarnings("error") or likewise). """ idn = threading.current_thread().ident if not sys.getprofile() is None: warn("restore_profiler: Current profile is not None!") if not idn in _saved_profilers: warn("restore_profiler: No saved profiler for calling thread!") else: sys.setprofile(_saved_profilers[idn]) del _saved_profilers[idn]
Example #3
Source File: type_util.py From pytypes with Apache License 2.0 | 6 votes |
def start(self): if self._active: raise RuntimeError('type checker already running') elif self._pending: raise RuntimeError('type checker already starting up') self._pending = True # Install this instance as the current profiler self._previous_profiler = sys.getprofile() self._set_caller_level_shift(0) sys.setprofile(self) # If requested, set this instance as the default profiler for all future threads # (does not affect existing threads) if self.all_threads: self._previous_thread_profiler = threading._profile_hook threading.setprofile(self) self._active, self._pending = True, False
Example #4
Source File: type_util.py From pytypes with Apache License 2.0 | 6 votes |
def stop(self): if self._active and not self._pending: self._pending = True if sys.getprofile() is self: sys.setprofile(self._previous_profiler) if not self._previous_profiler is None and \ isinstance(self._previous_profiler, TypeAgent): self._previous_profiler._set_caller_level_shift(0) else: if sys.getprofile() is not None or not self._cleared: warn('the system profiling hook has changed unexpectedly') if self.all_threads: if threading._profile_hook is self: threading.setprofile(self._previous_thread_profiler) else: # pragma: no cover warn('the threading profiling hook has changed unexpectedly') self._active, self._pending = False, False
Example #5
Source File: samplers.py From profiling with BSD 3-Clause "New" or "Revised" License | 5 votes |
def run(self, profiler): profile = functools.partial(self._profile, profiler) with deferral() as defer: sys.setprofile(profile) defer(sys.setprofile, None) threading.setprofile(profile) defer(threading.setprofile, None) yield
Example #6
Source File: __init__.py From profiling with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _profile(self, frame, event, arg): """The callback function to register by :func:`sys.setprofile`.""" # c = event.startswith('c_') if event.startswith('c_'): return time1 = self.timer() frames = self.frame_stack(frame) if frames: frames.pop() parent_stats = self.stats for f in frames: parent_stats = parent_stats.ensure_child(f.f_code, void) code = frame.f_code frame_key = id(frame) # if c: # event = event[2:] # code = mock_code(arg.__name__) # frame_key = id(arg) # record time2 = self.timer() self.overhead += time2 - time1 if event == 'call': time = time2 - self.overhead self.record_entering(time, code, frame_key, parent_stats) elif event == 'return': time = time1 - self.overhead self.record_leaving(time, code, frame_key, parent_stats) time3 = self.timer() self.overhead += time3 - time2
Example #7
Source File: collect_types.py From pyannotate with Apache License 2.0 | 5 votes |
def init_types_collection(filter_filename=default_filter_filename): # type: (Callable[[Optional[str]], Optional[str]]) -> None """ Setup profiler hooks to enable type collection. Call this one time from the main thread. The optional argument is a filter that maps a filename (from code.co_filename) to either a normalized filename or None. For the default filter see default_filter_filename(). """ global _filter_filename _filter_filename = filter_filename sys.setprofile(_trace_dispatch) threading.setprofile(_trace_dispatch)
Example #8
Source File: collect_types.py From pyannotate with Apache License 2.0 | 5 votes |
def stop_types_collection(): # type: () -> None """ Remove profiler hooks. """ sys.setprofile(None) threading.setprofile(None) # type: ignore
Example #9
Source File: Util.py From variety with GNU General Public License v3.0 | 5 votes |
def start(self): """ Starts the module profiler for all future threads. """ threading.setprofile(self.profiler)
Example #10
Source File: Util.py From variety with GNU General Public License v3.0 | 5 votes |
def stop(self): """ Removes the module profiler globally and from future threads. """ if sys.getprofile() != self.profiler: logger.warning( "ModuleProfiler: The currently enabled profile function was not ours - unbinding anyways" ) threading.setprofile(None) sys.setprofile(None)
Example #11
Source File: pytracing.py From pytracing with MIT License | 5 votes |
def install(self): """Install the trace function and open the JSON output stream.""" self.writer.start() # Start the writer thread. sys.setprofile(self.tracer) # Set the trace/profile function. threading.setprofile(self.tracer) # Set the trace/profile function for threads.
Example #12
Source File: pytracing.py From pytracing with MIT License | 5 votes |
def shutdown(self): sys.setprofile(None) # Clear the trace/profile function. threading.setprofile(None) # Clear the trace/profile function for threads. self.terminator.set() # Stop the writer thread. self.writer.join() # Join the writer thread.
Example #13
Source File: profile.py From dizzy with BSD 3-Clause "New" or "Revised" License | 5 votes |
def profile_on(): global p_stats, p_start_time p_stats = {} p_start_time = time() threading.setprofile(profiler) sys.setprofile(profiler)
Example #14
Source File: profile.py From dizzy with BSD 3-Clause "New" or "Revised" License | 5 votes |
def profile_off(): threading.setprofile(None) sys.setprofile(None)
Example #15
Source File: type_util.py From pytypes with Apache License 2.0 | 4 votes |
def __call__(self, frame, event, arg): if not self._active and not self._pending: # This happens if all_threads was enabled and a thread was created when the checker was # running but was then stopped. The thread's profiler callback can't be reset any other # way but this. sys.setprofile(self._previous_thread_profiler) return if self._pending: if self._previous_profiler is not None: self._previous_profiler(frame, event, arg) else: # If an actual profiler is running, don't include the type checking times in its results if event == 'call': if self._is_checking(): try: #check_argument_types(caller_level=self._caller_level_shift+1) _check_caller_type(False, caller_level=self._caller_level_shift+1) except RuntimeError: # Caller could not be determined. pass except pytypes.TypeCheckError: _saved_profilers[threading.current_thread().ident] = self self._cleared = True raise if self._previous_profiler is not None: self._previous_profiler(frame, event, arg) elif event == 'return': if self._previous_profiler is not None: self._previous_profiler(frame, event, arg) if self._is_checking(): try: _check_caller_type(True, None, arg, caller_level=self._caller_level_shift+1) except RuntimeError: # Caller could not be determined. pass except pytypes.TypeCheckError: _saved_profilers[threading.current_thread().ident] = self self._cleared = True raise except TypeError: # Caller could not be determined. pass if self._is_logging(): try: cllable, clss, slf, clsm, prop, prop_getter = \ _get_current_call_info(caller_level=self._caller_level_shift+1) act_func = util._actualfunc(cllable) specs = util.getargspecs(act_func) call_args = util.get_current_args(self._caller_level_shift+1, cllable, util.getargnames(specs)) if slf or clsm: call_args = call_args[1:] pytypes.log_type(call_args, arg, cllable if prop is None else prop, slf, prop_getter, clss, specs) except: pass else: if self._previous_profiler is not None: self._previous_profiler(frame, event, arg)