Source code for pylablib.core.thread.profile

from . import controller, threadprop
import time

try:
    import yappi
except ImportError:
    yappi=None



def _check_yappi():
    if yappi is None:
        msg=(   "operation requires yappi library. You can install it via PyPi as 'pip install yappi'. "
                "If it is installed, check if it imports correctly by running 'import yappi'")
        raise ImportError(msg)


_wall_timer=time.time()
[docs] def start(reset=True): # pylint: disable=redefined-outer-name """ Start yappi profile logging. If ``reset==True``, reset the stats. """ _check_yappi() global _wall_timer # pylint: disable=global-statement yappi.set_clock_type("cpu") yappi.stop() if reset: yappi.clear_stats() _wall_timer=time.time() yappi.start()
[docs] def reset(): """Reset yappi profiling stats""" _check_yappi() global _wall_timer # pylint: disable=global-statement yappi.clear_stats() _wall_timer=time.time()
[docs] def stop(): """Stop yappi profiling""" _check_yappi() yappi.stop()
[docs] def get_stats(): """ Get yappi profiling stats. Return tuple ``((ttime,wtime), (threads,ctls))``. Here ``ttime`` and ``wtime`` are total execution time (sum of all thread times) and the wall time (since the last reset) respectively. ``threads`` are yappi-generated stats, and ``ctls`` is the list ``[(name,ctl)]`` with the controller names and thread controllers, which are ordered in the same way as ``threads`` (for any non-controlled or stopped thread these are set to ``None``). """ _check_yappi() threads=yappi.get_thread_stats() ttime=sum(th.ttot for th in threads) wtime=time.time()-_wall_timer ctls=[] for th in threads: try: ctl=controller.get_controller(th.tid,sync=False) ctl_name=ctl.name except threadprop.NoControllerThreadError: ctl=None ctl_name=None ctls.append((ctl_name,ctl)) return (ttime,wtime),(threads,ctls)