dongzhu0327 2015-05-29 08:27
浏览 51
已采纳

如何在PHP中等待信号时阻止进程被阻止

I`m writing a Timer script,using a signal.Every 1 second send a SIGALRM to the process,then catch the signal and check whether the there is a task to do. I use pcntl_alarm(1) to send the signal,but the process excute-and-quit,before receive the signal. I think whether there is a method to set the prcess blocked waiting for the signal.I have used base_event,but its much heavy. So anybody can help me.Thanks a lot.

That`s my script(Sorry for the chinesse ^_^):

<?php
/**
*定时器
*/
class Timer
{
    //保存所有定时任务
    public static $task = array();

    //定时间隔
    public static $time = 1;

    /**
    *开启服务
    *@param $time int
    */
    public static function run($time = null)
    {
        if($time)
        {
            self::$time = $time;
        }
        self::installHandler();
        pcntl_alarm(1);
        do{ sleep(2); }while(true);
     }
    /**
    *注册信号处理函数
    */
    public static function installHandler()
    {
        echo "installHandler
";
        pcntl_signal(SIGALRM, array('Timer','signalHandler'),false);
        pcntl_alarm(1);
    }

    /**
    *信号处理函数
    */
    public static function signalHandler()
    {
        self::task();
    }

    /**
    *执行回调
    */
    public function task()
    {
        if(empty(self::$task))
        {
            return ;
        }
        foreach(self::$task as $time => $arr)
        {
            $current = time();
            $func = $arr['func'];
            $argv = $arr['argv'];
            $interval = $arr['interval'];
            $persist = $arr['persist'];

            if($current == $time)
            {
               call_user_func_array($func, $argv);
            }
            if($persist)
            {
               self::$task[$current+$interval] = $arr;
            }
            unset(self::$task[$time]);
        }
        pcntl_alarm(self::$time);
    }

    /**
    *添加任务
    public static function add($interval, $func, $argv,$persist =     false)
    {
        if(is_null($interval))
        {
            return;
        }
        $time = time()+$interval;
        self::$task[$time] = array('func'=>$func, 'argv'=>$argv, 'interval'=>$interval, 'persist'=>$persist);
    }

    /**
    *删除所有定时器任务
    */
    public function dellAll()
    {
        self::$task = array();
    }
}
  • 写回答

1条回答 默认 最新

  • duanci19881003 2015-07-29 08:05
    关注

    Now, I know the answer even though so many times has passed away for I had other jobs to finish...sorry for the late.So my answer is very simple.

    sleep

    is right for blocking a process, then the process receives a signal,and what I have missed is never capture it...so use

    pcntl_signal_dispatch

    function in a loop is exactly the way.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!