本文整理汇总了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";
}
}
示例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;
}
示例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;
}
示例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();
}
示例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));
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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]);
}
}
示例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);
}
示例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;
}
}
}
示例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");
});
}
示例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;
});
}