SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,180
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)

    Thoughts on large / long script

    Paracoding here, I have something like this:
    Code PHP:
    //curl_req.php
    class curl_req {
       public function getResults($arr) {
        //boring logic, make curl request, arrary push results, then return the array
       }
     
    }
     
    //index.php
    set_time_limit(0);
    require_once 'inc/curl_req.php';
    $arr = array(
    "1",
    "2"
    );
    $ver = new curl_req();
    $ver->getResults($arr);
    //print results

    Works like a charm, I print the results to a table in the web browser, set the time limit to 0 for larger files. My problem:

    These files have to be run from a remote linux box. I want to be able to do an ajax request or similar to check the status, is this possible? How?

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I think you mean how to store the status of the last fetch. Either on the File System, in a DB or Tweet it or something like that?

    If its the file system, how about index.php writes a file, call it status.php containing json equivalent to the likes of this:

    PHP Code:
    $results['time'] = 1112223334456;
    $results['job'][0] = "Done";
    $results['job'][1] = "Done";
    $results['job'][2] = "Problem"
    Then ajaxially call index.php?status=yes and it returns the json variables?

    Would that work?

  3. #3
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,180
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    getResults has a foreach() within it, this is what takes the longest. I want to know how far this iteration is, it doesnt matter to me where the data is stored, but I dont think I should be writing a SQL statement in every iteration to show an update of status.

  4. #4
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    I've never actually used it before, but why don't you look into something like Gearman? http://gearman.org/

    I think you're able to report on the progress of a task and stuff like that, and as far as I'm aware you could trigger this off via an ajax call and update when the task is finished. As I say I've not used Gearman myself, but I think it may do what you need?

    *Edit: http://php.net/manual/en/gearmanclient.jobstatus.php

    "Gets the status for a background job given a job handle. The status information will specify whether the job is known, whether the job is currently running, and the percentage completion. "

  5. #5
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    getResults has a foreach() within it, this is what takes the longest. I want to know how far this iteration is, it doesnt matter to me where the data is stored, but I dont think I should be writing a SQL statement in every iteration to show an update of status.
    An SQL query on every iteration might slow down too much but why not execute it, say, every 5 seconds? Would that be accurate enough for you? Some untested code sample, it shouldn't be slow:
    PHP Code:
    $start time();

    foreach (
    $data as $item) {
      if (
    time() - $start >= 5) {
        
    $start time();

        
    // update status (SQL, file, etc.)
        // ...
      
    }

      
    // do your stuff...

    Or, update status every 1000th iteration:
    PHP Code:
    // suppose that $data is numerically indexed
    foreach ($data as $i => $item) {
      if (
    $i 1000 == 0) {
        
    // update status (SQL, file, etc.)
        // ...
      
    }

      
    // do your stuff...


  6. #6
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,180
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    I like it, thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •