Andrew Waters
Andrew Waters

Reputation: 73

PHP forking delay processing

I have a cron job which runs every minute and executes internal cron logic for a multi tenanted application. The PHP script gets each client, their domains and then forks itself and runs under each client "scope" (internal name). See below:

<?php

$i = 0;
foreach($clients as $client) {
    $client->get_domains();
    foreach($client->domains as $domain) {
        $threads[$i]['client'] = $client->id;
        $threads[$i]['domain'] = $domain->id;
        $i++;
    }
}

$pids = array();
foreach($threads as $key => $thread) {
    $pids[$key] = pcntl_fork(); 
    if(!$pids[$key]) {
        $start = microtime(TRUE);
        $handle = popen($args[0] . ' --client ' . $thread['client'] . ' --domain ' . $thread['domain'] . ' 2>&1', 'r');
        // $end1 = ~0.001 sec execution time
        pclose($handle);
        // $end2 = ~60 sec execution time
        exit();
    }
}

I'm currently migrating load to another server, so have a fresh Ubuntu 11.04 install and set up like the existing machine. There are around 40 domains running on the app, so 40 forks are created within a second.

My issue is that event with each of the forked processes simply returning TRUE the processes are taking around 60 seconds.

When dumping $end1 time, the popen() is taking around 0.001 seconds, as to be expected. However, when monitoring $end2 the pclose() is taking ~60 seconds.

Watching htop, the processes are visible, but seem to be paused. There are 4 CPU's and 16GB RAM in the VM and load on the server is under 0.1 while running. Running PHP 5.3.6 on Ubuntu 11.10.

I have a feeling that it is something on the OS / PHP config but ulimit is normal and PHP memory limit and max execution time are unlimited. Is there anything else I should be checking?

Upvotes: 1

Views: 285

Answers (1)

David Schwartz
David Schwartz

Reputation: 182893

As the documentation says, "The pclose() function waits for the associated process to terminate." Your design really doesn't make any sense on many levels. There seems to be no reason whatsoever to use threads or to use popen. Is there more code to come that makes these decisions sensible? Because this really looks like an "H-bomb to kill an ant" program. Just use fork/exec.

Upvotes: 1

Related Questions