douhuike3199 2012-08-13 12:35
浏览 46

PHP分叉延迟处理

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?

  • 写回答

1条回答 默认 最新

  • dro44817 2012-08-13 12:41
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算