Giter Club home page Giter Club logo

py-xtimeout's Introduction

Check function with timeout for tracing or handle it

Feature

  • Check a function call is timeout or not.
  • The timeout callback and function call are on the same thread.
  • Multi-thread support. Nest call support.

Installation

pip install xtimeout

Usage

def on_timeout(start_time: float):
    """
    :param start_time: 
    """
    traceback.print_stack()
    pdb.set_trace()
    raise Exception("time_out")

# time unit is millisecond
@pymonitor.check_time(10, on_timeout)
def function_1():
    pass

def function_2():
    with pymonitor.check_context(20, on_timeout):
        # do something
        with pymonitor.check_context(10, on_timeout):
            # do something

Implementation Comparison

Here are some comparisons of the other implementations.

  • Use signal module and emit a signal

    • Only works in main thread.
    • Not good for nest call becauseof one signal correspond one handler.
      If you need nest support you need to enter the timeout function continually
      and call alarm. The cost depend on your accuracy.
    • Support Linux only.
  • Start new thread for work and join it with a time, if it had timeout, handle with it (eg. terminate it)

    • Can't inject the function call.
    • Overhead from threading.
  • Use sys.settrace keep tracing for each

    • A huge cost for that.

py-xtimeout's People

Contributors

amos402 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.