SoFunction
Updated on 2025-03-02

Python limits the execution time of function and implements timeout by yourself

Python limits the execution time of function and implements timeout by yourself

Updated: January 12, 2019 16:33:59 Author: Wu Kun
Today, the editor will share with you an example of python limiting the execution time of function and implementing timeout by yourself. It has good reference value and hope it will be helpful to everyone. Let's take a look with the editor

As shown below:

# coding=utf-8
import signal
import time
 
 
def set_timeout(num, callback):
  def wrap(func):
    def handle(signum, frame): # The callback function after receiving the signal SIGALRM, the first parameter is the number of the signal, and the second parameter is the interrupted stack frame.      raise RuntimeError
 
    def to_do(*args, **kwargs):
      try:
        (, handle) # Set signal and callback function        (num) # Set the alarm clock for num seconds        print 'start alarm signal.'
        r = func(*args, **kwargs)
        print 'close alarm signal.'
        (0) # Turn off the alarm clock        return r
      except RuntimeError as e:
        callback()
 
    return to_do
 
  return wrap
 
 
if __name__ == '__main__':
  def after_timeout(): # Processing function after timeout    print "do something after timeout."
 
 
  @set_timeout(2, after_timeout) # Limited time 2 seconds  def connect(): # Function to execute    (1) # Function execution time, write a value greater than 2, and test the timeout    return 'connect success.'
 
 
  print connect()

The above example of python limiting the execution time of function and implementing timeout by yourself is all the content I share with you. I hope you can give you a reference and I hope you can support me more.

  • python
  • function
  • timeout

Related Articles

  • OpenCV-Python implements nostalgic filters and comic strip filters

    Many times, many effects can be done through PS. Today we will introduce the use of OpenCV-Python to implement nostalgic filters and comic filters. They have certain reference value. Those who are interested can learn about it.
    2021-06-06
  • How to use Jupyter under VSCode

    This article mainly introduces how to use Jupyter under VSCode. This article introduces you very detailedly and has certain reference value for your study or work. Friends who need it can refer to it.
    2020-07-07
  • Detailed explanation of the usage of the exploit function in pandas dataframe

    This article mainly introduces a detailed explanation of the usage of the exploit function in pandas dataframe, which is of good reference value and hopes to be helpful to everyone. Let's take a look with the editor
    2020-05-05
  • An example of filtering the django background admin drop-down box

    Today, the editor will share with you an example of filtering the admin drop-down box of the django background. It has good reference value and hope it will be helpful to everyone. Let's take a look with the editor
    2019-07-07
  • In-depth analysis of JSON comparison in Python

    When performing interface automation, sometimes we need to assert more data, and it is more troublesome to assert one field at a time. So how to use Python to easily determine whether the data format is the same? The following editor will explain in detail.
    2023-09-09
  • Explanation of examples of python automatic email sending

    In this article, the editor has shared with you an example explanation about python automated email sending. Interested friends can learn to refer to it.
    2021-01-01
  • Analysis of comparison examples of map and list comprehension efficiency in Python

    This article mainly introduces the comparison of map and list comprehension efficiency in Python. The examples analyze the derivation efficiency of map and list in Python. Friends who need it can refer to it.
    2015-06-06
  • The Simple Bayesian Chapter of Python Machine Learning Applications

    The Naive Bayes model is a very simple and fast set of classification algorithms, usually suitable for data sets with very high dimensions. Because it runs fast and has few adjustable parameters, it is very suitable for providing fast and rough basic solutions for classification problems.
    2022-01-01
  • Give examples to explain deadlocks, reentrant locks and mutex locks in Python

    This article mainly introduces examples to explain deadlocks, reentrant locks and mutex locks in Python. Although Python's GIL problem in thread programming is common... If you need it, please refer to it.
    2015-11-11
  • Python3 uses logging package, how to write logs to the system's rsyslog

    This article mainly introduces the problem of using the logging package of Python3 to write logs to the system's rsyslog. It has good reference value. I hope it will be helpful to everyone. If there are any errors or no complete considerations, I hope you will be very encouraged.
    2023-09-09

Latest Comments