Web Crawler-PHP delayed execution problem?

Source: Internet
Author: User
Tags usleep
I checked it online. First, the sleep series (sleepusleepnanosleeptime_sleep_until) functions. First, these functions have a problem. sleep is suspended by the current process, for example: {code ...} in fact, after [wait for 10 seconds], [and at the same time] Two timestamps are displayed, timestamps... I checked it online. The first is the sleep series (sleep/usleep/nanosleep/time_sleep_until) functions. First, these functions have a problem. sleep is suspended by the current process, for example:

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

Actually 【Wait 10 secondsAfter ,【At the same timeTwo timestamps are displayed, with a difference of 10 seconds.

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

So:

I want to use an infinite loop and then determine the difference between the current time and the pre-stored time to break the current endless loop. For example:

For (100 times) {// some operations $ start = time (); while (true) {if (time ()-$ start = 1) break ;}}

It means that every time a for loop is executed, it enters an endless loop at the end, and then jumps out of the endless loop every second to continue the next for loop.
However, it is an endless loop !!!

Then I tried:

while(true){echo time();}

Not changed !!! The date ('s') generated by echo is changed !!!

So I want to ask:

1. If we need to meet the demand for delayed execution, we should not sleep or thread, simply pause and execute again (in fact, I am a crawler and the frequency will be 302 if it is too high ), is there any other way besides the above endless loop? (Do not use AJAX, only on the PHP side)

2. Why does that time () remain unchanged?

Reply content:

I checked it online. The first is the sleep series (sleep/usleep/nanosleep/time_sleep_until) functions. First, these functions have a problem. sleep is suspended by the current process, for example:

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

Actually 【Wait 10 secondsAfter ,【At the same timeTwo timestamps are displayed, with a difference of 10 seconds.

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

So:

I want to use an infinite loop and then determine the difference between the current time and the pre-stored time to break the current endless loop. For example:

For (100 times) {// some operations $ start = time (); while (true) {if (time ()-$ start = 1) break ;}}

It means that every time a for loop is executed, it enters an endless loop at the end, and then jumps out of the endless loop every second to continue the next for loop.
However, it is an endless loop !!!

Then I tried:

while(true){echo time();}

Not changed !!! The date ('s') generated by echo is changed !!!

So I want to ask:

1. If we need to meet the demand for delayed execution, we should not sleep or thread, simply pause and execute again (in fact, I am a crawler and the frequency will be 302 if it is too high ), is there any other way besides the above endless loop? (Do not use AJAX, only on the PHP side)

2. Why does that time () remain unchanged?

  1. Sleep does pause the program. After all, the two timestamps you get differ by 10 seconds, but why are they displayed after 10 seconds? Because the content output for the first time is still in the buffer, close the output cache to see what you want.ob_end_flush();

  2. In such a loop, hundreds of thousands of times can be output within one second. Of course, they are all the same in the same second. It will certainly change after one second.

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.