Does PHP sleep () consume a lot of resources?

Source: Internet
Author: User
Recent encounters with large data volumes and fast updates have resulted in a slight delay in synchronization of two servers and a higher load.

If using sleep to sleep 1 seconds every 10 seconds, would it relieve the pressure on the database? I'd like to know if sleep is going to take up a lot of resources and what happens when sleep executes?
But some people say that the longer single-threaded sleep takes more resources, is that it?
What if you were to verify how to cut it?
Online search on the sleep of the information but did not find that there is too much value, I really can't figure out how to solve the problem

Reply content:

Recent encounters with large data volumes and fast updates have resulted in a slight delay in synchronization of two servers and a higher load.

If using sleep to sleep 1 seconds every 10 seconds, would it relieve the pressure on the database? I'd like to know if sleep is going to take up a lot of resources and what happens when sleep executes?
But some people say that the longer single-threaded sleep takes more resources, is that it?
What if you were to verify how to cut it?
Online search on the sleep of the information but did not find that there is too much value, I really can't figure out how to solve the problem

Sleep does not consume system resources, it switches the CPU control to the kernel, and the kernel schedule to another process.

This simple thing, you write a while (true) sleep (1) program and then the top can be judged.

In any language, sleep itself does not account for CPU resources, but in a PHP-based lamp environment, this cannot be said, because the CPU is not the only resource. Number of processes, memory, these are resources.

PHP no matter the work in Apache mod_php or Nginx fpm, the total number of processes is basically a limit. For server throughput, there is a very simple arithmetic, you have a few threads/processes, you can process how many HTTP requests at the same time

However, a single sleep in PHP, then PHP processing the request time is equal to its own processing time +sleep time

Originally 0.2 seconds to deal with a request, now sleep for 0.8 seconds, then your PHP request processing speed becomes 1 seconds, assuming that your Apache open 10 processes, then you processing requests per second from 5 per second *10 process 50 to 1 per second of *10 process.

In addition, you can not open an unlimited process in Apache, regardless of the process or the thread, the overhead is very large, 1024 processes basically died very miserably, the thread in the 5k (CPU use is not much, most sleep case) also very laborious

If you sleep 10 seconds, the entire PHP, the web system can not be used, there were people who want to sleep to make a long connection, the native test passed, but the deployment of the system is tantamount to suicide.

Before also used PHP to write the background aspects of the program, the main process needs to wait for all the child process to exit, before the "busy", found that the main process will be the CPU, and then the main process in the waiting loop using sleep (1) way, greatly reducing the CPU overhead, So I think sleep should not take up too much CPU. For the landlord reference only!

@felix021 and @KJ are right, you need to evaluate what resources are being consumed in sleep, such as HTTP connections, database connections. In particular, the maximum number of concurrent connections to a database is usually less than an HTTP connection and requires careful connection to be eaten full. If it is PHP executed under the script, then sleep is a very simple and reliable method, if it is the web, you need to be cautious.

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