The situation is that I now have a lot of classes, and all the methods of each class will run aside and not be asynchronous. This can cause the script to take a long time to execute if each class executes too long.
Ask if you can add a maximum execution time to each class, and call a class method immediately after that time to return.
Reply content:
The situation is that I now have a lot of classes, and all the methods of each class will run aside and not be asynchronous. This can cause the script to take a long time to execute if each class executes too long.
Ask if you can add a maximum execution time to each class, and call a class method immediately after that time to return.
PHP itself is single-threaded, time-out detection is a multi-threaded logic, introduced a support multi-threaded class to start a detection thread on it.
In addition to the detection can only be specific methods or execution process, not a generic class, see your code how to write.
--(note: see the first comment for the actual answer)
A platform that supports PCNTL extensions can look at this http://php.net/manual/en/function.pcntl-alarm.php
The above Pcntl really is a relatively close solution, but I still think this business design is problematic, do not know the specific needs, so some thinking direction:
- Is there a problem with the business logic structure? Can the method be taken too long to improve? can improve
- Do you consider using other technologies to achieve better?
- You say more than a certain amount of time to return directly, whether the method you need to implement whether the implementation of the real business has no big impact? If not, why design this way?