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; }
/** * 启动服务 */ public static function start() { if (file_exists(self::$pid_file)) { syslog(LOG_INFO, 'pid file [' . self::$pid_file . '] is exists'); return; } //后台运行 \swoole_process::daemon(); //设置进程名称 \swoole_set_process_name(self::$process_name); self::get_pid(); self::write_pid(); self::registerSignal(); self::startUp(); defined('PHPKIT_RUN_DEBUG') && syslog(LOG_INFO, self::$process_name . ' start success'); }
public function checkStatus() { $args = getopt('s:'); if (isset($args['s'])) { switch ($args['s']) { case 'reload': $pid = file_get_contents($this->cacheDir . "/pid"); echo "当前进程" . $pid . "\n"; echo "热重启中\n"; if ($pid) { if (swoole_process::kill($pid, 0)) { swoole_process::kill($pid, SIGUSR1); } } echo "重启完成\n"; swoole_process::daemon(); break; default: break; } exit; } }
/** *是否以守护进程的方式运行 */ private static function daemon() { if (self::$daemon === true) { swoole_process::daemon(); } }
/** * Created by PhpStorm. * User: xiemin * Date: 2016/1/29 * Time: 14:49 */ date_default_timezone_set("Asia/Shanghai"); $cfg = (require_once './config.php'); $workers = []; /** * daemon($nochdir, $noclose); * $nochdir,为true表示不修改当前目录。默认false表示将当前目录切换到“/” * $noclose,默认false表示将标准输入和输出重定向到/dev/null */ if ($cfg && isset($cfg['debug']) && !$cfg['debug']) { swoole_process::daemon(1, 0); } /** * $function,子进程创建成功后要执行的函数 * $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);
<?php swoole_process::daemon(); while (1) { echo "hello"; sleep(1); }
$process = new swoole_process('rpcserver_call', true); $pid = $process->start(); function rpcserver_call(swoole_process $worker) { define('APPLICATION_PATH', dirname(dirname(__DIR__)) . "/application"); define('THRIFT_DIR_PATH', dirname(APPLICATION_PATH) . "/thrift"); require_once THRIFT_DIR_PATH . "/Thrift/ClassLoader/ThriftClassLoader.php"; $loader = new Thrift\ClassLoader\ThriftClassLoader(); $loader->registerNamespace('Thrift', THRIFT_DIR_PATH); $loader->registerNamespace('swoole', THRIFT_DIR_PATH); $loader->registerNamespace('Bin', THRIFT_DIR_PATH); $loader->registerDefinition('Bin', THRIFT_DIR_PATH); $loader->register(); define('MYPATH', dirname(APPLICATION_PATH)); $application = new Yaf_Application(dirname(APPLICATION_PATH) . "/conf/application.ini"); $application->bootstrap(); $config_obj = Yaf_Registry::get("config"); $rpc_config = $config_obj->rpc->toArray(); define('SERVERIP', $rpc_config['ServerIp']); define('SERVERPORT', $rpc_config['port']); define('SERVERHOST', $rpc_config['host']); $service = new Bin\rpc\Handler(); $processor = new Bin\rpc\rpcProcessor($service); $socket_tranport = new Thrift\Server\TServerSocket(SERVERIP, SERVERPORT); $out_factory = $in_factory = new Thrift\Factory\TFramedTransportFactory(); $out_protocol = $in_protocol = new Thrift\Factory\TBinaryProtocolFactory(); $server = new swoole\RpcServer($processor, $socket_tranport, $in_factory, $out_factory, $in_protocol, $out_protocol); $server->serve(); } swoole_process::daemon(true); swoole_process::wait();
/** * [run description] * @param Closure $logic 执行的业务逻辑 * @param boolean $nochdir 为true表示不修改当前目录。默认false表示将当前目录切换到“/” * @param boolean $noclose 默认false表示将标准输入和输出重定向到/dev/null * @return [type] [description] */ public static function run(Closure $logic, $nochdir = false, $noclose = false) { swoole_process::daemon($nochdir, $noclose); return call_user_func($logic); }
public static function start() { if (file_exists(self::$pid_file)) { die("Pid文件已存在!\n"); exit; } swoole_process::daemon(); $process = new swoole_process(array(new Main(), "http_run")); $process->start(); self::$http_server = $process; if (!function_exists("posix_getpid")) { Main::log_write("Please install posix extension." . "\n"); exit; } Main::log_write("dddd "); self::$pid = posix_getpid(); Main::log_write("pid " . self::$pid); file_put_contents(self::$pid_file, self::$pid); }
/** * @param int $worker_num * @param bool $daemon * @throws Exception\NotFound */ function runWorker($worker_num = 1, $daemon = false) { if ($worker_num > 1 or $daemon) { if (!class_exists('\\swoole\\process')) { throw new Exception\NotFound("require swoole extension"); } if ($worker_num < 0 or $worker_num > 1000) { $worker_num = 200; } } else { $this->_atomic = new \swoole_atomic(1); $this->_worker(); return; } if ($daemon) { \swoole_process::daemon(); } $this->_atomic = new \swoole_atomic(1); for ($i = 0; $i < $worker_num; $i++) { $process = new \swoole\process(array($this, '_worker'), false, false); $process->start(); $this->_workers[] = $process; } \swoole_process::signal(SIGCHLD, function () { while (true) { $exitProcess = \swoole_process::wait(false); if ($exitProcess) { foreach ($this->_workers as $k => $p) { if ($p->pid == $exitProcess['pid']) { if ($this->_atomic->get() == 1) { $p->start(); } else { unset($this->_workers[$k]); if (count($this->_workers) == 0) { swoole_event_exit(); } } } } } else { break; } } }); \swoole_process::signal(SIGTERM, function () { //停止运行 $this->_atomic->set(0); }); }
/** * @param bool $nochdir * @param bool $noclose * @return mixed */ function process_daemon($nochdir = true, $noclose = true) { return swoole_process::daemon($nochdir, $noclose); }