I want to easily implement a timeout in python

A library that can be used when you want to use a timeout in a library that does not implement timeout in Python

timeout-decorator https://github.com/pnpnpn/timeout-decorator

sample

test_timeout.py


import time
import timeout_decorator


def very_long_function():
    for i in range(100):
        print i
        time.sleep(1)


@timeout_decorator.timeout(5)
def test():
    very_long_function()


if __name__ == '__main__':
    try:
        test()
    except:
        print "test timed out :("
    else:
        print "test finished successfully :)"

Execution result

$ python test_timeoput.py
0
1
2
3
4
test timed out :(

Recommended Posts

I want to easily implement a timeout in python
I want to create a window in Python
I want to embed a variable in a Python string
I want to write in Python! (2) Let's write a test
I tried to implement a pseudo pachislot in Python
I want to randomly sample a file in Python
I want to work with a robot in python.
I tried to implement a one-dimensional cellular automaton in Python
I want to make input () a nice complement in python
I tried to implement PLSA in Python
I tried to implement permutation in Python
I want to print in a comprehension
I tried to implement PLSA in Python 2
I want to build a Python environment
I tried to implement ADALINE in Python
I tried to implement PPO in Python
I tried to implement a misunderstood prisoner's dilemma game in Python
I want to do Dunnett's test in Python
I want to easily create a Noise Model
I want to make a game with Python
I want to merge nested dicts in Python
I tried to implement TOPIC MODEL in Python
I want to easily find a delicious restaurant
I tried to implement selection sort in python
I want to write to a file with Python
I want to display the progress in Python!
I want to convert a table converted to PDF in Python back to CSV
I want to color a part of an Excel string in Python
I tried to implement a card game of playing cards in Python
I want to do a monkey patch only partially safely in Python
I want to iterate a Python generator many times
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to transition with a button in flask
Even in JavaScript, I want to see Python `range ()`!
I tried to implement Dragon Quest poker in Python
I want to easily build a model-based development environment
I tried to implement GA (genetic algorithm) in Python
[Python] I want to make a nested list a tuple
I want to write in Python! (3) Utilize the mock
I want to use the R dataset in python
I want to run a quantum computer with Python
I want to do something in Python when I finish
I want to manipulate strings in Kotlin like Python!
I tried to create a class that can easily serialize Json in Python
I want to create a priority queue that can be updated in Python (2.7)
Python program is slow! I want to speed up! In such a case ...
I tried to implement what seems to be a Windows snipping tool in Python
I want to debug with Python
I want to easily delete columns containing NA in R
I want to do something like sort uniq in Python
[Python] I want to get a common set between numpy
I want to start a lot of processes from python
I tried "How to get a method decorated in Python"
I want to send a message from Python to LINE Bot
I tried to implement the mail sending function in Python
I tried to make a stopwatch using tkinter in python
I want to be able to run Python in VS Code
A story about trying to implement a private variable in Python.
I tried to implement blackjack of card game in Python
I want to use a python data source in Re: Dash to get query results
I want to write a triple loop and conditional branch in one line in python