Web crawler-PHP delayed execution problem?

Source: Internet
Author: User
Tags usleep
Go online Check, first is the Sleep series (sleep/usleep/nanosleep/time_sleep_until) function, first of these functions have a problem, sleep is the current process hangs, such as:

echo time();sleep(10);echo time();

In fact, " wait for 10 seconds ", " again " Display two timestamp, the time difference between timestamps 10 seconds.

And the effect I want is to show the first timestamp first, wait 10 seconds, and then show the second timestamp.

So:

I want to break out of the current dead loop by using an infinite loop and then judging the difference between the current time and the stored time. Like what:

for(100遍){//一些操作$start = time();while(true){if(time() - $start == 1)break;}}

Meaning the For loop executes every time, at the end it goes into a dead loop, and then jumps out of the loop for 1 seconds, continuing the next for loop.
But, however, incredibly is the cycle of death!!!

Then tried it:

while(true){echo time();}

Incredibly unchanged!!! and echo out date (' s ') is changed!!!

So I want to ask:

1, if the need to meet the delay in the implementation of the demand, do not sleep, do not thread, simply is suspended under the execution (in fact, I do the crawler, high frequency will be 302), in addition to the above dead cycle of the way there are other ways? (Not AJAX, PHP only)

2. Why is the time () unchanged?

Reply content:

Go online Check, first is the Sleep series (sleep/usleep/nanosleep/time_sleep_until) function, first of these functions have a problem, sleep is the current process hangs, such as:

echo time();sleep(10);echo time();

In fact, " wait for 10 seconds ", " again " Display two timestamp, the time difference between timestamps 10 seconds.

And the effect I want is to show the first timestamp first, wait 10 seconds, and then show the second timestamp.

So:

I want to break out of the current dead loop by using an infinite loop and then judging the difference between the current time and the stored time. Like what:

for(100遍){//一些操作$start = time();while(true){if(time() - $start == 1)break;}}

Meaning the For loop executes every time, at the end it goes into a dead loop, and then jumps out of the loop for 1 seconds, continuing the next for loop.
But, however, incredibly is the cycle of death!!!

Then tried it:

while(true){echo time();}

Incredibly unchanged!!! and echo out date (' s ') is changed!!!

So I want to ask:

1, if the need to meet the delay in the implementation of the demand, do not sleep, do not thread, simply is suspended under the execution (in fact, I do the crawler, high frequency will be 302), in addition to the above dead cycle of the way there are other ways? (Not AJAX, PHP only)

2. Why is the time () unchanged?

    1. Sleep does allow the program to pause, after all, you get two timestamp difference 10 seconds, but why is 10s after the show it together? Because the content of the first output is still in buffer, turn off the output cache to see the effect you wantob_end_flush();

    2. Such a cycle, 1 seconds can output hundreds of thousands of millions of times, the same second of course, the same, 1s will certainly change.

  • Related Article

    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.