Does PHPsleep () occupy a lot of resources?

Source: Internet
Author: User
Recently, when the data volume is large and the update speed is fast, the synchronization between the two servers is delayed and the load increases. If sleep is used to sleep for 1 second every 10 seconds, can it relieve the pressure on the database? I 'd like to know if sleep will occupy a lot of resources, sl... recently, when the data volume is large and the update speed is fast, the synchronization between the two servers is delayed and the load increases.

If sleep is used to sleep for 1 second every 10 seconds, can it relieve the pressure on the database? I 'd like to know if sleep will occupy a lot of resources. what did sleep do during execution?
But some people say that the longer the sleep time of a single thread, the more resources it occupies. Is that true?
If I verify it myself, how can I cut in?
I searched for sleep information on the internet but did not find any great value. I cannot figure out how to solve it.

Reply content:

Recently, when the data volume is large and the update speed is fast, the synchronization between the two servers is delayed and the load increases.

If sleep is used to sleep for 1 second every 10 seconds, can it relieve the pressure on the database? I 'd like to know if sleep will occupy a lot of resources. what did sleep do during execution?
But some people say that the longer the sleep time of a single thread, the more resources it occupies. Is that true?
If I verify it myself, how can I cut in?
I searched for sleep information on the internet but did not find any great value. I cannot figure out how to solve it.

Sleep does not occupy system resources. it switches control of the cpu to the kernel and schedule the kernel to another process.

In this simple case, you can write a while (true) sleep (1) program and then top it to judge.

Sleep does not occupy CPU resources in any language, but it cannot be said in a PHP-based LAMP environment because the CPU is not the only resource. the number of processes and memory are resources.

Regardless of whether PHP is working in apache mod_php or nginx fpm, the total number of processes is basically limited. the server throughput is very simple. you have several threads/processes, and you can process the number of http requests at the same time.

However, if a request is sleep in PHP, the request processing time in php is equal to the processing time + sleep time.

If 0.2 seconds is used to process a request and sleep for 0.8 seconds, the processing speed of your php request will be 1 second. assume that your apache has launched 10 processes, then, the request processing speed is changed from 5 processes per second * 10 processes 50 to 1*10 processes per second.

In addition, you cannot open infinite processes in apache. both processes and threads have high overhead, and 1024 processes are basically dead, the thread is about 5 KB (CPU usage is not much, and most sleep cases ).

If you sleep for 10 seconds, the entire PHP and web system won't work anymore. some people once wanted to use sleep for persistent connections, and the local machine passed the test, but deploying the system would be tantamount to committing suicide.

I have also used php to write background programs. The main process needs to wait for all sub-processes to end and then exit. Previously, I used "busy" and found that the main process has occupied the CPU, later, the main process uses sleep (1) in the waiting loop, greatly reducing the cpu overhead. Therefore, I think sleep should not occupy too much CPU. For reference only!

@ Felix021 and @ KJ are both correct. you need to evaluate the resources occupied in sleep, such as HTTP connections and database connections. In particular, the maximum number of concurrent connections to the database is usually less than that of HTTP connections. be careful when the connections are full. If it is PHP executed in the script, sleep is a very simple and reliable method; if it is web, you need to be careful.

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.