preamble
When we use the thread pool to control the thread running, or write a crawler that keeps sending requests to get the address, we need to control the running thread. One such scenario is when you write a website request, the thread keeps requesting and not responding causing the thread to block, wasting precious thread resources. When you write an incorrect sql query, the query keeps running and takes a lot of time, causing other queries to block. In cases such as the above you must control the threads to make the program script more stable.
There are several ways to control the running time of a thread:
There are two methods described here, one is to do it with the method of the eventlet library, which expires as soon as a subroutine call is made. The other method is to use the @func_set_timeout modifier, which can be applied to a series of subfunctions such as functions and threads, and is the most convenient, simple and efficient method. Here is a code manipulation demonstrating these two methods.
I. Eventlet
The library can be downloaded directly:
pip install eventlet import time import eventlet#Import eventlet eventlet.monkey_patch()# Introduce patch with (2,False):# Set the timeout to 2 seconds (3) print('1') print('2')
We set the sleep time to 3 seconds, which is more than 2 seconds, which causes the execution of the statement print('1') to skip and output 2 directly:
import time import eventlet#Import eventlet eventlet.monkey_patch()# Introduce patch with (2,False):# Set the timeout to 2 seconds (1) print('1') print('2')
When we modify it so that 1 is less than 2, there is no timeout, and we should output 1 and 2 at this point:
When we want to call the subroutine the function is not working. Like this, the function has no effect and this is when we need to use the second method.
II. func-timeout
1. Installation
Just install it directly.
pip install func-timeout
2. Use
It's usually used with try except, which throws an error if it times out.
from func_timeout import func_set_timeout import func_timeout @func_set_timeout(1)# Set function timeout execution time def task(i): (2) print(i) try: task(1) except: print(2)
At this point sleep time over 1 should output 2:
from func_timeout import func_set_timeout import func_timeout @func_set_timeout(3)# Set function timeout execution time def task(i): (2) print(i) try: task(1) except: print(2)
This results in an output of 1.
To this article on Python control thread and function timeout processing is introduced to this article, more related Python control thread content please search my previous posts or continue to browse the following articles hope that you will support me more!