Search code examples
phptwitterfreebsdbackground-processbsd

PHP Background Process on BSD uses 100% CPU


I have a PHP script that runs as a background process. This script simply uses fopen to read from the Twitter Streaming API. Essentially an http connection that never ends. I can't post the script unfortunately because it is proprietary. The script on Ubuntu runs normally and uses very little CPU. However on BSD the script always uses nearly a 100% CPU. The script is working just fine on both machines and is the exact same script. Can anyone think of something that might point me in the right direction to fix this? This is the first PHP script I have written to consistently run in the background.

The script is an infinite loop, it reads the data out and writes to a json file every minute. The script will write to a MySQL database whenever a reconnect happens, which is usually after days of running. The script does nothing else and is not very long. I have little experience with BSD or writing PHP scripts that run infinite loops. Thanks in advance for any suggestions, let me know if this belongs in another StackExchange. I will try to answer any questions as quickly as possible, because I realize the question is very vague.


Solution

  • Without seeing the script, this is very difficult to give you a definitive answer, however what you need to do is ensure that your script is waiting for data appropriately. What you should absolutely definitely not do is call stream_set_timeout($fp, 0); or stream_set_blocking($fp, 0); on your file pointer.

    The basic structure of a script to do something like this that should avoid racing would be something like this:

    // Open the file pointer and set blocking mode
    $fp = fopen('http://www.domain.tld/somepage.file','r');
    stream_set_timeout($fp, 1);
    stream_set_blocking($fp, 1);
    
    while (!feof($fp)) { // This should loop until the server closes the connection
    
      // This line should be pretty much the first line in the loop
      // It will try and fetch a line from $fp, and block for 1 second
      // or until one is available. This should help avoid racing
      // You can also use fread() in the same way if necessary
      if (($str = fgets($fp)) === FALSE) continue;
    
      // rest of app logic goes here
    
    }
    

    You can use sleep()/usleep() to avoid racing as well, but the better approach is to rely on a blocking function call to do your blocking. If it works on one OS but not on another, try setting the blocking modes/behaviour explicitly, as above.

    If you can't get this to work with a call to fopen() passing a HTTP URL, it may be a problem with the HTTP wrapper implementation in PHP. To work around this, you could use fsockopen() and handle the request yourself. This is not too difficult, especially if you only need to send a single request and read a constant stream response.