本文整理汇总了PHP中swoole_process::start方法的典型用法代码示例。如果您正苦于以下问题:PHP swoole_process::start方法的具体用法?PHP swoole_process::start怎么用?PHP swoole_process::start使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类swoole_process
的用法示例。
在下文中一共展示了swoole_process::start方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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;
}
示例2: 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;
}
示例3: run
function run()
{
//swoole_process::daemon(0, 1);
$this->start_time = $this->middle_time = microtime(true);
if ($this->status == 'import') {
$db = new mysqli('127.0.0.1', 'root', '', 'friend');
//$db = new mysqli('119.254.111.25', 'myroot', 'jXIN@274.com', 'friend', 60002);
$sql = "SELECT COUNT(*) FROM friend";
if ($result = $db->query($sql)) {
$row = $result->fetch_row();
$this->data_row = $row[0];
$result->free();
}
$db->close();
echo 'total data count: ' . $this->data_row . PHP_EOL;
}
//$this->data_row = 100000;
for ($i = 0; $i < $this->worker_num; $i++) {
$process = new swoole_process(array($this, 'child_async'), $this->redirect_stdout);
$process->id = $i;
$pid = $process->start();
$this->workers[$pid] = $process;
//echo "Master: new worker, PID=".$pid."\n";
}
$this->master_async();
}
示例4: onMessage
public function onMessage(swoole_websocket_server $server, $frame)
{
var_dump($frame->data);
$data = json_decode($frame->data, true);
var_dump($data);
$cmd = $data['cmd'];
$is_block = isset($data['is_block']) ? $data['is_block'] : 0;
if ($is_block) {
if (isset($this->async_process[$frame->fd])) {
$process = $this->async_process[$frame->fd];
} else {
$process = new swoole_process(array($this, 'onTmpProcess'), true, 2);
$process->start();
$this->async_process[$frame->fd] = $process;
swoole_event_add($process->pipe, function () use($process, $frame) {
$data = $process->read();
var_dump($data);
$this->server->push($frame->fd, $data);
});
}
$process->write($cmd);
sleep(1);
} else {
$this->process->write($cmd);
$data = $this->process->read();
$this->server->push($frame->fd, $data);
}
}
示例5: 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";
}
}
示例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: 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);
}
示例8: callback_timer_function
function callback_timer_function()
{
$Handle = new TimingCall_Tasks();
$Handle->upDaemonTime();
$tasks = $Handle->readExecuteTasks();
foreach ($tasks as $task) {
$process = new swoole_process('callback_process_function', true);
$process->write(json_encode($task));
$process->start();
$process->wait(false);
}
}
示例9: 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]);
}
}
示例10: run
public function run()
{
$this->current_num = $this->min_worker_num;
for ($i = 0; $i < $this->current_num; $i++) {
$process = new swoole_process(array($this, 'task_run'), false, 2);
$pid = $process->start();
$this->process_list[$pid] = $process;
$this->process_use[$pid] = 0;
}
foreach ($this->process_list as $process) {
swoole_event_add($process->pipe, function ($pipe) use($process) {
$data = $process->read();
var_dump($data);
$this->process_use[$data] = 0;
});
}
swoole_timer_tick(1000, function ($timer_id) {
static $index = 0;
$index = $index + 1;
$flag = true;
foreach ($this->process_use as $pid => $used) {
if ($used == 0) {
$flag = false;
$this->process_use[$pid] = 1;
$this->process_list[$pid]->write($index . "Hello");
break;
}
}
if ($flag && $this->current_num < $this->max_worker_num) {
$process = new swoole_process(array($this, 'task_run'), false, 2);
$pid = $process->start();
$this->process_list[$pid] = $process;
$this->process_use[$pid] = 1;
$this->process_list[$pid]->write($index . "Hello");
$this->current_num++;
}
var_dump($index);
if ($index == 10) {
foreach ($this->process_list as $process) {
$process->write("exit");
}
swoole_timer_clear($timer_id);
$this->process->exit();
}
});
}
示例11: memoryCheck
protected function memoryCheck()
{
$process = new \swoole_process(function (\swoole_process $worker) {
$worker->name("kerisy-rpcserver:memoryCheck");
swoole_timer_tick(1000, function () {
$serverName = "kerisy-rpcserver:master";
Reload::load($serverName, 0.8);
// echo date('H:i:s')."\r\n";
if (date('H:i') == '04:00') {
Reload::reload($serverName);
//休息70s
sleep(70);
}
});
});
$process->start();
}
示例12: createWorkerProcess
public static function createWorkerProcess($className, $number, $options)
{
$reflector = new \ReflectionClass($className);
if (!$reflector->implementsInterface("PHPKit\\Console\\Workers\\IWorker")) {
defined('PHPKIT_RUN_DEBUG') && syslog(LOG_ERR, 'class [' . $className . '] is error');
self::stop();
} else {
$process = new \swoole_process(function ($worker) use($className, $number, $options) {
$worker->name(self::$child_process_name . str_replace('\\', '_', $className) . '_' . $number);
$handler = new $className($options);
$handler->tick($worker);
});
$pid = $process->start();
self::$worker_list[$pid] = ['className' => $className, 'number' => $number, 'process' => $process, 'options' => $options];
defined('PHPKIT_RUN_DEBUG') && syslog(LOG_INFO, 'create child process success:' . $pid);
}
}
示例13: start
/**
* @brief 启动子进程
*/
public function start()
{
$workerNum = $this->_objAha->getConfig()->get('aha', 'worker_num');
for ($i = 0; $i < $workerNum; $i++) {
$worker = new \Daemon\Asyncworker($this->_objAha);
$process = new \swoole_process(array($worker, 'start'));
//$process->daemon();
$workerPid = $process->start();
$this->_workers[$workerPid] = $process;
$process->write("worker started!");
}
foreach ($this->_workers as $process) {
$workerPid = \swoole_process::wait();
echo "[Worker Shutdown][WorkerId] {$workerPid} " . PHP_EOL;
unset($this->_workers[$workerPid]);
}
}
示例14: 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, "type" => "crontab", "process" => $process);
swoole_event_add($process->pipe, function ($pipe) use($process) {
$task = $process->read();
list($pid, $sec) = explode(",", $task);
if (isset(Crontab::$task_list[$pid])) {
$tasklist = Crontab::$task_list[$pid];
Crontab::$delay[time() + $sec] = $tasklist["task"];
$process->write($task);
}
});
}
示例15: logprint
* $redirect_stdin_stdout,重定向子进程的标准输入和输出。 启用此选项后,在进程内echo将不是打印屏幕,而是写入到管道。读取键盘输入将变为从管道中读取数据。 默认为阻塞读取。
* $create_pipe,是否创建管道,启用$redirect_stdin_stdout后,此选项将忽略用户参数,强制为true 如果子进程内没有进程间通信,可以设置为false
*/
if ($cfg && isset($cfg['muti_process']) && is_array($cfg['muti_process'])) {
foreach ($cfg['muti_process'] as $proc_cfg) {
$process = new swoole_process(function (swoole_process $worker) use($proc_cfg) {
swoole_timer_tick($proc_cfg['ms'], function ($timer_id) use($proc_cfg) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $proc_cfg['url']);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
logprint('info', trim($data));
});
}, false, false);
$pid = $process->start();
$workers[$pid] = $process;
}
} else {
logprint('warn', 'cfg is invaild');
}
master_process($workers);
function logprint($level, $msg)
{
global $cfg;
if ($cfg['debug'] == true) {
echo '[' . date("Y/m/d H:i:s") . '] [' . $level . '] [' . posix_getpid() . '] [' . $msg . ']' . PHP_EOL;
} else {
file_put_contents($cfg['logfile'], '[' . date("Y/m/d H:i:s") . '] [' . $level . '] [' . posix_getpid() . '] [' . $msg . ']' . PHP_EOL, FILE_APPEND | LOCK_EX);
}
}