當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。