I have a script that gets to a portion where it uses web services and this portion of the script slows down considerably because of the slowness of the web service. The service is out of my control so I can't do anything about the speed of it. I was wondering if as a solution I could create some sort of forked process so it could send additional requests to the web service while it is still processing any previous requests rather than going through sequentially and not starting a new request until the last request has completed like I have it now.

Thanks.

    I think by forked process, you mean running multiple threads.

    PHP does not support threaded programming.

    Does the information that you retrieve from the webservice change greatly? could you cache it? Maybe even use a cron job to store the information from the web service in a local database. This could run hourly or something depending how often the information changes.

    The approach you takes to this depends on what you are getting from the web service. If you describe that a bit more we can advise you better.

      We don't quite know what you need, but I want to offer up this for an example of pseudo realtime. You can use this to emulate multi-threaded stuff in web applications.

      I have a script like this set up to run on init after everything else. I even use start-stop-daemon to keep an eye on it.

      <?php
      /** eventd.php -- Event Daemon */
      set_time_limit(0);
      error_reporting(E_ALL);
      
      if (!substr(SAPI_NAME, 0, 3) == 'cli')
      {
          die('You must run this from the command line!');
      }
      
      $counter = time();
      
      while (true)
      {
      	// Sleeps are good for the processor
      	sleep(1);
      
      // Don't call time() every second.
      $counter += 1; 
      
      if ($counter % 2 == 0)
      {
      	// $counter is an *even* number...
      	// DO SOMETHING HERE	
      }
      else
      {
      	// $counter is an *odd* number...
      	// DO SOMETHING HERE	
      }
      
      
      if ($counter % 60 == 0)
      {
      	// Counter is a multiple of 60.  Resync
      	// the timer so we don't get too far off
      	$counter = time();
      }
      
      } // main loop
      ?>
      

      If you don't want to run it every second, then increase the sleep statement. Note, however, if you are running stuff only once a minute or slower, cron may be a better idea (as dougal85 said).

      This technique uses very little cpu time and memory on my debian box, even at 20 requests per second on the web side.

      Have your web process send raw data to a mysql heap table or other fast storage. Then set up this script to read from it and do all the hard work. Only let it do a couple things each pass (hence why I broke it up to even and odd). Make sure the code is error free as possible (try/catch!) and set it free.

      tail -f php_errors.log & pray --faithfully

        Write a Reply...