Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add timeout for Python function under Linux/Mac

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to add timeout for Python function under Linux/Mac". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to add timeouts for Python functions under Linux/Mac".

First, let's take a look at how this signal is used:

Import time import signal def handler (signum, _): print ('timing!') Raise Exception ('time is up!') Def clac_statistic (datas): time.sleep (100) signal.signal (signal.SIGALRM, handler) signal.alarm (5) clac_statistic ('xxx')

The running effect is shown in the following figure:

First bind the signal.SIGALRM event to the handler function, and then use signal.alarm (10) to delay sending a signal for 10 seconds. Ten seconds later, the function handler is run. An exception was thrown in the function, causing the program to end. The clac_statistic function was supposed to run for 100 seconds, but it stopped after 10 seconds, thus realizing the timeout function of the function.

Based on the above principles, we implement a decorator to simplify setting timeouts for different functions:

Import time import signal class FuncTimeoutException (Exception): pass def handler (signum, _): raise FuncTimeoutException ('function time is up!') Def func_timeout (times=0): def decorator (func): if not times: return func def wraps (* args, * * kwargs): signal.alarm (times) result = func (* args, * * kwargs) signal.alarm (0) # function is completed ahead of schedule Cancel signal return result return wraps return decorator signal.signal (signal.SIGALRM, handler)

Let's give it a try and test the function timeout decorator. First of all, when the running time of the test function is less than the timeout, there is no problem with the normal operation of the program:

Let's test the situation where the function runs longer than the timeout:

A FuncTimeoutException exception is thrown normally.

In practice, we can use try...except FuncTimeoutException to catch this exception, and then implement a custom handling flow, such as:

Try: clac_statistic except FuncTimeException: print ('this function runs timeout, run custom processing flow') so far, I believe you have a deeper understanding of "how to add timeout for Python function under Linux/Mac". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report