On 30/10/2019 12:02, Marcos Passos wrote:
Hi everyone,
We're looking for a way to run a code block with a timeout.
Our use case involves evaluating an expression written in a custom DSL
(similar to Symfony Expression Language), and we want to ensure that
the execution will not exceed a given time limit.
We tried a naive approach using set_time_limit, but there is no way to
catch the exception from the block code that timed out. We
then considered using PCNTL, but its use is discouraged production
environments.
Do you guys have any suggestions?
- Marcos
off the top of my head:-
- can you not set globally scoped (or passed as params) timer and time
limit before the code block and the code block itself returns/exits when
it hits the limit?
- if the above is not appropriate/possible and this may be a bit hacky
for your purposes but have you considered/tried implementing a tick
function?
https://www.php.net/manual/en/function.register-tick-function.phpand
https://stackoverflow.com/questions/7493676/detecting-a-timeout-for-a-block-of-code-in-php/7498006#7498006