PHP limits the execution time of a piece of code

Source: Internet
Author: User
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:

  1. Is there a problem with the business logic structure? Can the method be taken too long to improve? can improve
  2. Do you consider using other technologies to achieve better?
  3. 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?
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.