$http->close();关闭连接,close后如果再次请求get、post等方法时,底层会重新连接服务器
来源:7-5 让Swoole完美支持TP5(下)
唐成勇
2018-04-26
<?php
$http = new swoole_http_server("0.0.0.0", 9911);
$http->set(
[
'enable_static_handler' => true,
'document_root' => "/home/wwwroot/swoole/thinkphp/public/static",
'worker_num' => 5,
]
);
//此事件在Worker进程/Task进程启动时发生,这里创建的对象可以在进程生命周期内使用
$http->on('WorkerStart', function(swoole_server $server, $worker_id) {
// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
// 加载框架里面的文件
require __DIR__ . '/../../../../thinkphp/base.php';
});
$http->on('request', function($request, $response) use($http){
/**
* 解决上一次输入的变量还存在的问题
* 方案一:if(!empty($_GET)) {unset($_GET);}
* 方案二:$http-close();把之前的进程kill,swoole会重新启一个进程,重启会释放内存,把上一次的资源包括变量等全部清空
* 方案三:$_SERVER = []
*/
//$_SERVER = [];
if(isset($request->server)) {
foreach($request->server as $k => $v) {
$_SERVER[strtoupper($k)] = $v;
}
}
if(isset($request->header)) {
foreach($request->header as $k => $v) {
$_SERVER[strtoupper($k)] = $v;
}
}
//$_GET = [];
if(isset($request->get)) {
foreach($request->get as $k => $v) {
$_GET[$k] = $v;
}
}
//$_POST = [];
if(isset($request->post)) {
foreach($request->post as $k => $v) {
$_POST[$k] = $v;
}
}
//开启缓冲区
ob_start();
// 执行应用并响应
try {
think\Container::get('app', [APP_PATH])
->run()
->send();
}catch (\Exception $e) {
// todo
}
//输出TP当前请求的控制方法
//echo "-action-".request()->action().PHP_EOL;
//获取缓冲区内容
$res = ob_get_contents();
ob_end_clean();
$response->end($res);
//把之前的进程kill,swoole会重新启一个进程,重启会释放内存,把上一次的资源包括变量等全部清空
$http->close();
});
$http->start();老师$http->close();把上一次的资源包括变量等全部清空,如果close了,为什么再次请求浏览器还可以执行成功,不应该把服务给关了吗?这个和php php_server.php没有关系吗?感觉有点绕~
写回答
3回答
-
唐成勇
提问者
2018-04-26
@singwa老师:
nginx配置如下:
server { listen 9501 default_server; root /home/wwwroot/swoole/thinkphp/public; #include other.conf; #error_page 404 /404.html; # Deny access to PHP files in specific directory #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; } location / { index index.html index.htm index.php; if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1; } } location ~ \.php { fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; fastcgi_param PHP_ADMIN_VALUE $basedir if_not_empty; set $basedir "open_basedir=/home/wwwroot/swoole/thinkphp/:tmp/:proc/"; fastcgi_param SCRIPT_FILENAME /home/wwwroot/swoole/thinkphp/public$fastcgi_script_name; } # include enable-php.conf; location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } location ~ /\. { deny all; } access_log /home/wwwlogs/swoole.log; }00 -
singwa
2018-04-26
您好。$http->close本来就是错误的用法,老师故意这样写,后面有优化,因为初学者很容易 写$http->close
052018-04-26 -
唐成勇
提问者
2018-04-26
哈哈,好像明白了~不知道理解的对不对:
$http->close();会把之前的worker进程kill,swoole会重新启一个worker进程,重启会释放内存,把上一次的资源包括变量等全部清空,和php_server.php没有关系,它只是php_server启动5个worker进程中的一个worker
00
相似问题