当前位置: 首页>>代码示例>>PHP>>正文


PHP swoole_process类代码示例

本文整理汇总了PHP中swoole_process的典型用法代码示例。如果您正苦于以下问题:PHP swoole_process类的具体用法?PHP swoole_process怎么用?PHP swoole_process使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了swoole_process类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: run

 static function run()
 {
     $table = new swoole_table(1024 * 256);
     $table->column('index', swoole_table::TYPE_INT);
     $table->column('serid', swoole_table::TYPE_INT);
     $table->column('data', swoole_table::TYPE_STRING, 64);
     $table->create();
     self::$table = $table;
     for ($i = 0; $i < self::$key_num; $i++) {
         $key = 'user_' . (self::$key_base + $i);
         $ret = self::$table->set($key, array('index' => $i, 'serid' => rand(1000, 9999), 'data' => "hello_world_{$i}"));
         if (!$ret) {
             echo "count {$i} failed.";
             break;
         }
     }
     for ($i = 0; $i < self::$worker_num; $i++) {
         $process = new swoole_process('UnitTest_Table::worker');
         $process->start();
         $workers[$i] = $process;
     }
     for ($i = 0; $i < self::$worker_num; $i++) {
         $exit = swoole_process::wait();
         echo "worker[{$i}] exit\n";
     }
 }
开发者ID:cjq,项目名称:tests,代码行数:26,代码来源:table.php

示例2: forkChildInWorker

function forkChildInWorker()
{
    global $serv;
    echo "on worker start\n";
    $process = new swoole_process(function (swoole_process $worker) {
        // 		$serv = new swoole_server( "0.0.0.0", 9503 );
        // 		$serv->set(array(
        // 				'worker_num' => 1
        // 		));
        // 		$serv->on ( 'receive', function (swoole_server $serv, $fd, $from_id, $data) {
        // 			$serv->send ( $fd, "Swoole: " . $data );
        // 			$serv->close ( $fd );
        // 		});
        // 		$serv->start ();
        // 		swoole_event_add ($worker->pipe, function ($pipe) use ($worker) {
        // 			echo $worker->read()."\n";
        // 		});
        // 		swoole_timer_add (1000, function ($interval) use ($worker) {
        // 			echo "#{$worker->pid} child process timer $interval\n"; // 如果worker中没有定时器,则会输出 process timer xxx
        // 		});
    });
    $pid = $process->start();
    echo "Fork child process success. pid={$pid}\n";
    //保存子进程对象,这里如果不保存,那对象会被销毁,管道也会被关闭
    $serv->childprocess = $process;
}
开发者ID:jinguanio,项目名称:swoolecrawler,代码行数:26,代码来源:server.php

示例3: run

 function run($options)
 {
     $asDaemon = isset($options['asDaemon']) ? $options['asDaemon'] : 0;
     if ($asDaemon) {
         \swoole_process::daemon();
     }
     $pids = [];
     $workers = [];
     for ($i = 0; $i < $this->worker_num; $i++) {
         $process = new \swoole_process($this->workerStart, $this->redirect_stdout);
         $process->id = $i;
         $pid = $process->start();
         $pids[] = $pid;
         $workers[$pid] = $process;
     }
     $pidFile = isset($options['pidFile']) ? $options['pidFile'] : 0;
     if ($pidFile) {
         $ppid = posix_getpid();
         $pids[] = $ppid;
         file_put_contents($pidFile, implode("|", $pids));
     }
     \swoole_process::signal(SIGTERM, function () use($workers) {
         exit(0);
     });
     \swoole_process::signal(SIGINT, function () {
         exit(0);
     });
     \swoole_process::wait(false);
     return $workers;
 }
开发者ID:kerisy,项目名称:framework,代码行数:30,代码来源:Process.php

示例4: start

 /**
  * @brief 启动子进程
  * @param \swoole_process $worker
  */
 public function start(\swoole_process $worker)
 {
     $this->_worker = $worker;
     swoole_event_add($worker->pipe, function ($pipe) use($worker) {
         echo $worker->read() . PHP_EOL;
     });
     $this->worker();
 }
开发者ID:vucms,项目名称:aha,代码行数:12,代码来源:Asyncworker.php

示例5: callback_vmstat

/**
 * swoole_process回调函数
 * @param swoole_process $worker
 */
function callback_vmstat(swoole_process $worker)
{
    global $table;
    $vmstat = $table->get('vmstat');
    $cmd = $vmstat['cmd'];
    $interval = $vmstat['interval'];
    $count = $vmstat['count'];
    $worker->exec($cmd, array($interval, $count));
}
开发者ID:stonegithubs,项目名称:swoole-vmstat,代码行数:13,代码来源:server.php

示例6: create_process

 /**
  * 创建一个子进程
  * @param $task
  */
 public function create_process($id, $task)
 {
     $this->task = $task;
     $process = new swoole_process(array($this, "run"));
     if (!($pid = $process->start())) {
     }
     //记录当前任务
     Crontab::$task_list[$pid] = array("start" => microtime(true), "id" => $id, "task" => $task);
 }
开发者ID:royalwang,项目名称:swoole-crontab,代码行数:13,代码来源:Process.class.php

示例7: callback_function

function callback_function(swoole_process $worker)
{
    //echo "Worker: start. PID=".$worker->pid."\n";
    //recv data from master
    $recv = $worker->pop();
    echo "From Master: {$recv}\n";
    sleep(2);
    $worker->exit(0);
}
开发者ID:chenwenbin928,项目名称:swoole-src,代码行数:9,代码来源:msgqueue.php

示例8: callback_function

function callback_function(swoole_process $worker)
{
    echo "Worker: start. PID=" . $worker->pid . "\n";
    //recv data from master
    $recv = $worker->read();
    echo "From Master: {$recv}\n";
    //send data to master
    $worker->write("hello master\n");
    sleep(2);
    $worker->exit(0);
}
开发者ID:vkey24,项目名称:crazy-grab,代码行数:11,代码来源:worker.php

示例9: callback_process_function

function callback_process_function(swoole_process $worker)
{
    set_time_limit(300);
    $task = json_decode($worker->read(), true);
    $worker->name('crontab.php worker=' . $task['uniqid']);
    $Handle = new TimingCall_Handle($task);
    $Handle->upNextExecuteTime();
    $Handle->refreshStatistics();
    $Handle->Execute();
    $worker->exit(0);
}
开发者ID:imdaqian,项目名称:Hi-Timed,代码行数:11,代码来源:cmd.php

示例10: create_process

 /**
  * 创建一个子进程
  * @param $classname
  * @param $number
  * @param $redis
  */
 public function create_process($classname, $number, $redis)
 {
     $this->workers["classname"] = $classname;
     $this->workers["number"] = $number;
     $this->workers["redis"] = $redis;
     $process = new swoole_process(array($this, "run"));
     if (!($pid = $process->start())) {
     }
     //记录当前任务
     Crontab::$task_list[$pid] = array("start" => microtime(true), "classname" => $classname, "number" => $number, "redis" => $redis, "type" => "worker", "process" => $process);
 }
开发者ID:noikiy,项目名称:swoole-crontab,代码行数:17,代码来源:Worker.class.php

示例11: worker_run

 private function worker_run()
 {
     for ($i = 0; $i < $this->process_number; $i++) {
         $process = new swoole_process(array($this, "worker_callback"), false, true);
         $pid = $process->start();
         self::$workers[$i] = $process;
         self::$workers_pipe[$process->pipe] = $process;
         self::$restart_event[$i] = event_new();
         //var_dump($process->pipe);
         event_set(self::$restart_event[$i], $process->pipe, EV_READ | EV_PERSIST, array($this, "relisten"));
         event_base_set(self::$restart_event[$i], $this->event_base);
         event_add(self::$restart_event[$i]);
     }
 }
开发者ID:zhangjingpu,项目名称:yaf-lib,代码行数:14,代码来源:Processpool.php

示例12: callback_function

function callback_function(swoole_process $worker)
{
    //echo "Worker: start. PID=".$worker->pid."\n";
    //recv data from master
    $cnt = 0;
    while ($cnt < 2) {
        // 如果队列里没有数据,则pop方法阻塞等待
        $recv = $worker->pop();
        echo "From Master: {$recv}\n";
        $cnt++;
    }
    sleep(1);
    $worker->exit(0);
}
开发者ID:silentred,项目名称:learning-path,代码行数:14,代码来源:queue.php

示例13: readPipe

 /**
  * @brief 读取管道中的消息包体
  * @param \swoole_process $process
  */
 public function readPipe(\swoole_process $process)
 {
     while (false !== ($content = $process->read())) {
         if (false === $content) {
             \Aha\Log\Sys::log()->error(array('IPC_PIPE_READ_ERR' => $content));
             break;
         } else {
             $this->append($content);
         }
         if (strlen($content) < 8191) {
             break;
         }
     }
 }
开发者ID:eyehere,项目名称:aha,代码行数:18,代码来源:Package.php

示例14: child_async

function child_async(swoole_process $worker)
{
    //echo "Worker: start. PID=".$worker->pid."\n";
    //recv data from master
    $GLOBALS['worker'] = $worker;
    global $argv;
    $worker->name("{$argv[0]}: worker");
    swoole_process::signal(SIGTERM, function ($signal_num) use($worker) {
        echo "signal call = {$signal_num}, #{$worker->pid}\n";
    });
    swoole_event_add($worker->pipe, function ($pipe) use($worker) {
        $recv = $worker->read();
        echo "From Master: {$recv}\n";
        $worker->write("hello master\n");
    });
}
开发者ID:stonegithubs,项目名称:swoole-game,代码行数:16,代码来源:server.php

示例15: master_process

function master_process($workers)
{
    //监听子进程,如果停止,会再拉起来
    swoole_process::signal(SIGCHLD, function ($signo) use(&$workers) {
        while (1) {
            $ret = swoole_process::wait(false);
            if ($ret) {
                $pid = $ret['pid'];
                //这里实现一个自动拉起的能力
                $child_process = $workers[$pid];
                logprint('info', "Worker Exit, kill_signal={$ret['signal']} PID=" . $pid);
                $new_pid = $child_process->start();
                $workers[$new_pid] = $child_process;
                unset($workers[$pid]);
            } else {
                break;
            }
        }
    });
    //kill -10 结束全部程序
    swoole_process::signal(SIGUSR1, function ($signo) use(&$workers) {
        swoole_process::signal(SIGCHLD, null);
        foreach ($workers as $pid => $worker) {
            swoole_process::kill($pid);
        }
        //处理子进程,然后自己退出
        exit;
    });
}
开发者ID:peterxiemin,项目名称:commonswoole,代码行数:29,代码来源:timer.php


注:本文中的swoole_process类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。