How can I tell how to set the function execution time?

Source: Internet
Author: User
Tags usleep
function A () {
//...
return true;
}
Function B () {
//...
return false;
}
Now start calling a ();

Imagine if a (), after 1.2 seconds did not return the result, immediately call B ();
What's the nice way to deal with it?


Reply to discussion (solution)


Don't be too yourself, and don't make PHP any harder.

My solution
1, Ajax time-out interpretation disadvantage: Time is not accurate advantage: This feature can be temporarily implemented
2, PHP's C language extension disadvantage: The difficulty is big cost-effective not high advantage: time accurate efficiency high
3. Wait for Daniel ....

Libevent can implement the function of the timer, but your a function should be to perform network tasks, if so, there must be a timeout setting

PHP itself provides this type of functionality
Whether it is beautiful, you judge by yourself
Not all operations are calculated in PHP run time, you can verify it yourself

Register_shutdown_function ("Time_out_callback"); Set_time_limit (2);//Assuming 2 seconds even timeout//analog timeout @c ();//error_reporting ($es) ;//Restore Set function Time_out_callback () {  if (connection_status () = = 2) {    B ();      }} Function C () {  //Your possible timeout code  echo __function__, ';  while (true) {    echo '. ';        Echo Connection_status (). "
";//echo 0 Usleep (100000);//Give CPU breath }}function B () { echo ')
' . __function__, "; Echo ' C Timeout connection status: '. Connection_status ();//echo 2,c Timeout}

PHP itself provides this type of functionality
Whether it is beautiful, you judge by yourself
Not all operations are calculated in PHP run time ...
Moderator to the force, Gray often beautiful, although the results are not very stable (change usleep value a result), PHP so the estimate is to the limit

  • 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.