Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 561019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:24:22+00:00 2026-05-13T12:24:22+00:00

I have a list of data that needs to be processed. The way it

  • 0

I have a list of data that needs to be processed. The way it works right now is this:

  • A user clicks a process button.
  • The PHP code takes the first item that needs to be processed, takes 15-25 secs to process it, moves on to the next item, and so on.

This takes way too long. What I’d like instead is that:

  • The user clicks the process button.
  • A PHP script takes the first item and starts to process it.
  • Simultaneously another instance of the script takes the next item and processes it.
  • And so on, so around 5-6 of the items are being process simultaneously and we get 6 items processed in 15-25 secs instead of just one.

Is something like this possible?

I was thinking that I use CRON to launch an instance of the script every second. All items that need to be processed will be flagged as such in the MySQL database, so whenever an instance is launched through CRON, it will simply take the next item flagged to be processed and remove the flag.

Thoughts?

Edit: To clarify something, each ‘item’ is stored in a mysql database table as seperate rows. Whenever processing starts on an item, it is flagged as being processed in the db, hence each new instance will simply grab the next row which is not being processed and process it. Hence I don’t have to supply the items as command line arguments.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:24:22+00:00Added an answer on May 13, 2026 at 12:24 pm

    Here’s one solution, not the greatest, but will work fine on Linux:

    Split the processing PHP into a separate CLI scripts in which:

    • The command line inputs include `$id` and `$item`
    • The script writes its PID to a file in `/tmp/$id.$item.pid`
    • The script echos results as XML or something that can be read into PHP to stdout
    • When finished the script deletes the `/tmp/$id.$item.pid` file

    Your master script (presumably on your webserver) would do:

    • `exec(“nohup php myprocessing.php $id $item > /tmp/$id.$item.xml”);` for each item
    • Poll the `/tmp/$id.$item.pid` files until all are deleted (sleep/check poll is enough)
    • If they are never deleted kill all the processing scripts and report failure
    • If successful read the from `/tmp/$id.$item.xml` for format/output to user
    • Delete the XML files if you don’t want to cache for later use

    A backgrounded nohup started application will run independent of the script that started it.

    This interested me sufficiently that I decided to write a POC.

    test.php

    <?php
    $dir =  realpath(dirname(__FILE__));
    $start = time();
    
    // Time in seconds after which we give up and kill everything
    $timeout = 25;
    
    // The unique identifier for the request
    $id = uniqid();
    
    // Our "items" which would be supplied by the user
    $items = array("foo", "bar", "0xdeadbeef");
    
    // We exec a nohup command that is backgrounded which returns immediately
    foreach ($items as $item) {
        exec("nohup php proc.php $id $item > $dir/proc.$id.$item.out &");
    }
    
    echo "<pre>";
    // Run until timeout or all processing has finished
    while(time() - $start < $timeout) 
    {
      echo (time() - $start), " seconds\n";
      clearstatcache();    // Required since PHP will cache for file_exists
      $running = array();
      foreach($items as $item)
      {
          // If the pid file still exists the process is still running    
          if (file_exists("$dir/proc.$id.$item.pid")) {
              $running[] = $item;
          }
      }
      if (empty($running)) break;
      echo implode($running, ','), " running\n";
      flush();
      sleep(1);  
    }
    
    // Clean up if we timeout out
    if (!empty($running)) {
        clearstatcache();
        foreach ($items as $item) {
            // Kill process of anything still running (i.e. that has a pid file)
            if(file_exists("$dir/proc.$id.$item.pid") 
                && $pid = file_get_contents("$dir/proc.$id.$item.pid")) {
                posix_kill($pid, 9);                
                unlink("$dir/proc.$id.$item.pid");
                // Would want to log this in the real world
                echo "Failed to process: ", $item, " pid ", $pid, "\n";
        }
        // delete the useless data
        unlink("$dir/proc.$id.$item.out");
        }
    } else {
        echo "Successfully processed all items in ", time() - $start, " seconds.\n";
        foreach ($items as $item) {
        // Grab the processed data and delete the file
            echo(file_get_contents("$dir/proc.$id.$item.out"));
            unlink("$dir/proc.$id.$item.out");
        }
    }
    echo "</pre>";
    ?>
    

    proc.php

    <?php
    $dir =  realpath(dirname(__FILE__));
    $id = $argv[1];
    $item = $argv[2];
    
    // Write out our pid file
    file_put_contents("$dir/proc.$id.$item.pid", posix_getpid());
    
    for($i=0;$i<80;++$i)
    {
        echo $item,':', $i, "\n";
        usleep(250000);
    }
    
    // Remove our pid file to say we're done processing
    unlink("proc.$id.$item.pid");
    
    ?>
    

    Put test.php and proc.php in the same folder of your server, load test.php and enjoy.

    You will of course need nohup (unix) and PHP cli to get this to work.

    Lots of fun, I may find a use for it later.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a webpage that displays a very large list of data. Since this
I have a list of data that looks like the following: // timestep,x_position,y_position 0,4,7
I have a list of data that is a schedule. Each item has a
I have a long list of data that I want to display in table
I have a rather large list of data that contains 5 properties per element.
I have a list control in Flex that has been data bound to an
i have a large list of static data from a server that i load
I have an ItemsControl that is data bound to a list of decimal s.
I have a test list that I am trying to capture data from using
I have a .NET repeater control that is data-bound to a List. As part

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.