Not just for the piece of code.
However, there are alternative solutions without changing the source code:
set_time_limit
set_time_limit() will abort the entire script if it runs too long. You can still configure the shutdown function.
pcntl_alarm
pcntl_alarm() will send you a signal after a specified period of time has elapsed, which may interrupt the lock lock that the class does at this time, and this may allow you to ask the class to abort if it provides such a method. However, this may not be acceptable in a web server environment.
Default Timeouts
If the class uses stream functions, you can set a default timeout:
ini_set('default_socket_timeout', 5); stream_context_set_default(array( 'http' => array( 'timeout' => 5, ), ));
Execution in a separate process
You can fork pcntl_fork() , but this does not work in a server environment.
You can also use proc_open or popen to execute a PHP script in a separate process and kill it if it runs too long. (After the process is spawned, wait for the process to wait on the stdout stream with stream_select .)
Or configure the server to work with these tasks.
source share